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:

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. action - This is the async task to be performed, which itself should return a Promise.

A couple things to note:

I simulated the async task with a simple setTimeout but this would be optimal for looping over Database calls, HTTP requests, or anything else requiring latency.

but this would be optimal for looping over Database calls, HTTP requests, or anything else requiring latency. We use a function for the condition which allows us to do any manner of interesting tests for completion.

We utilize process.nextTick to wait until the next CPU cycle before starting. If we don’t do this, it will complete immediately and will not “loop.”

to wait until the next CPU cycle before starting. If we don’t do this, it will complete immediately and will not “loop.” You’ll see it is actually using recursion for the iteration, but behaves like a while loop hence the title with the word “while loop” in quotes. But it does follow a while loop-esque format of condition, action, looping until condition is met.

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

var sum = 0 , stop = 10 ; promiseWhile ( function ( ) { return sum < stop ; } , function ( ) { return new Promise ( function ( resolve , reject ) { setTimeout ( function ( ) { sum ++ ; console . log ( sum ) ; resolve ( ) ; } , 250 ) ; } ) ; } ) . then ( function ( ) { 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!