Async

posted in: AngularJS, JavaScript | 1

This is a great how to guide: http://www.sebastianseilund.com/nodejs-async-in-practice

 

How do I wait for a number of API calls to complete with their response data before proceeding?

 

Use Async – https://github.com/caolan/async

 

In this example “renderListGroup()” will only fire when all the API calls have returned their data. “results” contains an array of arrays with all the results from all API calls.

 

    async.parallel({
            fetchOwners: function (callback) {
                $.getJSON("http://think-a-doo.net/ThinkadooAdmin/api/owners", function (data) {
                    callback(null, data);
                });
            },
            fetchPets: function (callback) {
                $.getJSON("http://think-a-doo.net/ThinkadooAdmin/api/pets", function (data) {
                    callback(null, data);
                });
            }
        },
        function (err, results) {
            renderListGroup (results);
        });

One Response

Leave a Reply