1

I have a function "add_floor(xxx)" in a loop (see bellow) that take some time to complete, so i would like to know how to wait for the return of this function before moving to the next step which is in my case "add dawfeature points". I understand that I could use a callback method but then I would need to restart the complete loop that goes over the json elements in the calledback function. Not really sexy, so is there a way to avoid this ?

function add_maps_from_json(json) {
  for (var i = 0; i < json.maps.length; i++) {
    // add the maps to the combox box and display last one 
    add_floor(json.maps[i].mapName);

    // add the dawfeature points to the map
    for (var j = 0; i < json.maps[i].APs.length; j++) {
      var idx = find_map_index(); 
      console.log(idx);
      var map = mapObjectArray[idx];
      etc...
      etc...

    }
  }
}
10
  • 3
    the only way it wouldn't just follow on from previous code is if you're creating anonymous functions or doing ajax calls or any timeout functions?? Commented Jan 13, 2014 at 16:49
  • 2
    Are there any AJAX calls here? Otherwise, this should be synchronous. Meaning it will wait until it's done before it returns. If you are using AJAX calls, the only way would be with callbacks. Commented Jan 13, 2014 at 16:50
  • What add_floor() does? Assync calls? Commented Jan 13, 2014 at 16:50
  • @RocketHazmat "synchronous" thank you, much better than 'follow on from previous code' lol Commented Jan 13, 2014 at 16:50
  • 1
    There's your problem! onload is an DOM event, DOM event handlers are called asynchronously. You'll probably need to rework this a bit, perhaps make add_floor return a promise which is resolved when all images are loaded and added. Commented Jan 13, 2014 at 16:58

3 Answers 3

2

You can make the function return a value to a variable. That way the program will pause until the function as run. Like this:

var maps = add_maps_from_json(json);

Or you can use plugin.js and do module calls.

http://requirejs.org/docs/plugins.html

Sign up to request clarification or add additional context in comments.

1 Comment

Hi the return value trick doesn't work, I will look at the plugin.js
1

as you have said you have to add a callback parameter to add_floor and for managing your loop you can do async loop, like this:

//change the add_floor function
function add_floor(mapName, callback){
    //your code
    //you probably have some kind of ajax call or setTimeout
    //then call your callback after your ajax response or timer callback
    //....
    callback();
    //....
}

function add_maps_from_json(json) {
    doLoop(json, 0)
}

function doLoop(json, i) {
    if(i < json.maps.length) return;
    add_floor(json.maps[i].mapName, function () {
        // add the dawfeature points to the map
        for (var j = 0; i < json.maps[i].APs.length; j++) {
            var idx = find_map_index();
            console.log(idx);
            var map = mapObjectArray[idx];
            //etc...
            //etc...

        }
        doLoop(json, i + 1);
    });
}

Comments

1

If add_floor doesn't do an ajax call, then your code will work fine as it is now.

If add_floor DOES do an ajax call, then you can put the inner for loop inside a separate function that you then use as the callback for the ajax call in add_floor.

1 Comment

There are other ways of creating a non-blocking call besides using Ajax--for instance, web workers, or using setTimeout (as the Clumpy library does to simulate non-blocking loops).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.