This is because AJAX is asynchronous. Nothing is going to wait for it to finish. The callback will run in the future at some point when the call is done. By then your $.each (and the code after) is long done.

The solution here is to use promises. That way you can run a callback once all the AJAX calls are done.

You can use jQuery's $.Deferred for this. Without editing the InputGen() function, you can do something like this: