JavaScript: Async Promise “while loop”  

This is a solution to the situation where you have an asynchronous task you want to perform over and over again, in a non-blocking fashion, stopping when some condition is met.

To set the stage, I’ll be using the Bluebird Promise library, the best Promise library I’ve used.

First, we’ll construct the Promise function which will do the looping:

var Promise = require('bluebird');

var promiseWhile = function(condition, action) {
    var resolver = Promise.defer();

    var loop = function() {
        if (!condition()) return resolver.resolve();
        return Promise.cast(action())
            .then(loop)
            .catch(resolver.reject);
    };

    process.nextTick(loop);

    return resolver.promise;
};

It receives 2 arguments, both expected to be functions:

  1. condition - This is a predicate function which shall return true or false and indicate whether the end state has been reached. true if done, false otherwise.
  2. action - This is the async task to be performed, which itself should return a Promise.

A couple things to note:

What follows is a sample usage of the promiseWhile() function.

// And below is a sample usage of this promiseWhile function
var sum = 0,
    stop = 10;

promiseWhile(function() {
    // Condition for stopping
    return sum < stop;
}, function() {
    // Action to run, should return a promise
    return new Promise(function(resolve, reject) {
        // Arbitrary 250ms async method to simulate async process
        // In real usage it could just be a normal async event that 
        // returns a Promise.
        setTimeout(function() {
            sum++;
            // Print out the sum thus far to show progress
            console.log(sum);
            resolve();
        }, 250);
    });
}).then(function() {
    // Notice we can chain it because it's a Promise, 
    // this will run after completion of the promiseWhile Promise!
    console.log("Done");
});

And that’s it!

I threw the whole gist on GitHub

Feel free to email me or Tweet me @victorquinn with any questions!

 
796
Kudos
 
796
Kudos

Now read this

Emacs: Prevent autosave mess

Emacs will create backup files for you. By default, it will do so after every 300 characters you type. 1 It does so by creating a new file with the same filename as the one you were editing, but wrapping it in # characters. So if you... Continue →