A small library to make asyncronous loops for Promise
based functions.
You could use it as a wrapper to do polling or if you would like to loop but the function is asyncronous.
The library is actually not dependent on any Promise
implementation and are only using .then
of the function you provide, therefore you could use any thenable promise.
Install and check API below
Install with npm
.
npm install while-promised
Require in you code.
var whilePromised = require('while-promised');
whilePromised(yourFunction);
let cnt = 0;
function counter() {
return new Promise(function(resolve, reject) {
setImmediate(resolve, ++cnt);
});
}
// Loop while resolved until is 6
whilePromised(counter, 6)
.then(function(value) {
console.log('Values is: ' + value);
});
This is the API of this library. The function returns a Promise
and is thenable for when the loop finishes.
whilePromised(fn)
.then(function() {
// Do something
});
The simplest while loop for resolved value !== false
.
whilePromised(fn);
This is comparable to:
var a = true;
while(a) {
a = fn();
}
Loop while resolved value !== stopVal
.
whilePromised(fn, stopVal);
This is comparable to:
var a = 0;
while(a !== 10) {
a = fn();
}
Loop while compareFn
returns true
whilePromised(fn, compareFn);
This is comparable to:
while(compareFn) {
fn();
}
Please do! Write in es6
and transpile to es5
with npm run build
.
MIT © Jacob Carlsson