I’ve spent bits of my spare time over the last weeks improving my JavaScript skills. I’ve read Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript, by David Herman; I highly recommend this book to any who wants to dig deeper into this language.

Last Thursday my pal @rafeca raised an interesting question: how could we start two or more asynchronous operations in JavaScript and execute a callback upon all of them are finished, but not before?

One of the latest chapters of the mentioned book comes up with the answer: store responses in an ordered array and execute the callback when every response has been received (there are no pending operations).

Let’s see an specific example: retrieve a set of user profiles from a third party database and print the result in an HTML table only when all of them have been received.

In the following five steps we’ll figure out how to resolve this:

1.- Create the HTML skeleton to print the user profiles

              <th class="name">Name</th>
              <th class="profile">Profile</th>
        <tbody id="profiles"></tbody>
    <div id="error">

2.- Create a simple function to retrieve user profiles

For the sake of simplicity, I’ve mocked the profile database using an in-memory dictionary:

var Profile = function(name, profile) {
    this.name = name;
    this.profile = profile;

var ProfileDB = (function() {
    var users = {
        john: new Profile("john", "manager"),
        mark: new Profile("mark", "developer"),
        thomas: new Profile("thomas", "QA")

    var _getUserProfile = function(user, callback, error) {
            var profile = users[user];
            if(typeof profile === "undefined") {
                error("profile " + user + " not found");
            else {
                if(callback) {
                    // simulate a random delay between 0 and 1 seconds
                    setTimeout(callback.bind(null, profile), 1000 * Math.random());
                else {
                    return users[user];

    return {
        getUserProfile: _getUserProfile


3.- Create a function to retrieve a set of users profile and execute a callback upon every profile retrieval

In this step we’re building the function that will start in parallel the required operations and execute the relevant callback (success or error in case of any failure):

function getUserProfiles(users, onsuccess, onerror) {
    // number of pending operations
    var pending = users.length;
    // store results in this array
    var result = [];
    if (pending === 0) {
        // execute callback if users is empty
        setTimeout(onsuccess.bind(null, result), 0);
    users.forEach(function(user, i) {
        ProfileDB.getUserProfile(user, function(profile) {
            if(result) {
                result[i] = profile;
            if(pending === 0) {
                // every profile has been retrieved, execute callback
        }, function(error) {
            // execute error callback

4.- Create the client to retrieve a list of user profiles

Step 4 creates the client that will use the function created in step 3. We need to provide callbacks for both success and error scenearios. Upon success, users profile are printed in the HTML skeleton built in step 1. In case of error, the specific message is shown in the div element:

var userElement = function(name, profile) {
    var el = document.createElement("tr");
    var _name = document.createElement("td");
    var _profile = document.createElement("td");
    return el;

(function() {
    getUserProfiles(["john", "mark", "thomas"], function(profiles) {
        var fragment = document.createDocumentFragment();
        profiles = profiles.forEach(function(profile) {
            fragment.appendChild(userElement(profile.name, profile.profile));
    }, function(error) {
        var el = document.getElementById("error");
        el.innerHTML= "";

5.- Run it

In this case we’re updating the DOM just once, upon retrieving the three users profile. This doesn’t provide a high advantage, but if we’re retrieving hundreds of elements, updating the DOM in any response may reduce significantly our application performance :squirrel:.

« Home