How can wait for ajax call end before relooping in foreach?

I am trying to use Promises but I don’t know if in this case, are useful.

$.getJSON("url", "data", function(json) {
    json.forEach(function(item) {
        //do something
        $.get("url", "data", function(response) {
            //MAKE SURE THIS BODY IS SOLVED BEFORE RELOOP???
        });
    })
})

The only way I found is to use ajax with async as false.

Is there a way to solve this?

Maybe using Promises for example? I tried to think a way to use Promises in a foreach loop but didn’t find any solution.

I tried this one:

$.getJSON("url", "data", function(json) {
    json.forEach(function(item) {
        //do something
        when($.get("url", "data") {
            //do something
        })).done(function(response){
            //do something
        });
    })
})

But it doesn’t work. foreach always relooped.

8 thoughts on “How can wait for ajax call end before relooping in foreach?”

  1. whoah this blog is magnificent i like reading your posts.
    Keep up the great work! You realize, lots of persons are searching around for this information, you can aid them greatly.

    Reply
  2. You’re so cool! I don’t think I have read through a single thing like this before.
    So great to discover someone with a few genuine thoughts on this subject.
    Really.. thanks for starting this up. This site is one
    thing that’s needed on the web, someone with a bit of originality!

    Reply

Leave a Comment