while-promised v1.0.3
while promised
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.
Usage
Install and check API below
Installation
Install with npm
.
npm install while-promised
Require in you code.
var whilePromised = require('while-promised');
whilePromised(yourFunction);
Example
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);
});
API
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
});
Resolve to false
The simplest while loop for resolved value !== false
.
whilePromised(fn);
This is comparable to:
var a = true;
while(a) {
a = fn();
}
Resolve to value
Loop while resolved value !== stopVal
.
whilePromised(fn, stopVal);
This is comparable to:
var a = 0;
while(a !== 10) {
a = fn();
}
Compare function
Loop while compareFn
returns true
whilePromised(fn, compareFn);
This is comparable to:
while(compareFn) {
fn();
}
Contribute
Please do! Write in es6
and transpile to es5
with npm run build
.
License
MIT © Jacob Carlsson