async-do-while
A simple library for Bluebird that helps you create while or do-while loops involving asynchronous operations.
One key design goal is maintaining readability. A loop is structured as multiple function calls so that you don't up end with awkwardly indented code.
Usage
A basic do-while loop:
var Async = ; var loop = 0;Async;Async;return Async;
The function passed to do()
will be called repeatedly, as long as the function passed to while()
returns a truthy value or a promise that resolves to a truthy value. end()
is the function that starts the loop. It returns a Bluebird promise. .
To do a while loop (where the continuation condition is checked first), simply flip the order by which do()
and while()
are called:
var loop = 0;Async;Async;return Async;
Use return()
to return a value (the value that the promise returned by end()
fulfills with):
var loop = 0;Async;Async;Async;return Async;
If something needs to happen at the beginning of the loop, call begin()
with a callback. Typically, you would use this only when you have nested loops.
To break out of a loop, call break()
. It throws an exception that is then trapped and ignored by end()
. The same Error object is thrown every time. It's exported from the module as AsyncBreak
.
var loop = 0;Async;Async;return Async;