Pend
Dead-simple optimistic async helper.
Usage
var Pend = require('pend');
var pend = new Pend();
pend.max = 10; // defaults to Infinity
setTimeout(pend.hold(), 1000); // pend.wait will have to wait for this hold to finish
pend.go(function(cb) {
console.log("this function is immediately executed");
setTimeout(function() {
console.log("calling cb 1");
cb();
}, 500);
});
pend.go(function(cb) {
console.log("this function is also immediately executed");
setTimeout(function() {
console.log("calling cb 2");
cb();
}, 1000);
});
pend.wait(function(err) {
console.log("this is excuted when the first 2 have returned.");
console.log("err is a possible error in the standard callback style.");
});
Output:
this function is immediately executed
this function is also immediately executed
calling cb 1
calling cb 2
this is excuted when the first 2 have returned.
err is a possible error in the standard callback style.
Source
| 1 | # Pend |
| 2 | |
| 3 | Dead-simple optimistic async helper. |
| 4 | |
| 5 | ## Usage |
| 6 | |
| 7 | ```js |
| 8 | var Pend = require('pend'); |
| 9 | var pend = new Pend(); |
| 10 | pend.max = 10; // defaults to Infinity |
| 11 | setTimeout(pend.hold(), 1000); // pend.wait will have to wait for this hold to finish |
| 12 | pend.go(function(cb) { |
| 13 | console.log("this function is immediately executed"); |
| 14 | setTimeout(function() { |
| 15 | console.log("calling cb 1"); |
| 16 | cb(); |
| 17 | }, 500); |
| 18 | }); |
| 19 | pend.go(function(cb) { |
| 20 | console.log("this function is also immediately executed"); |
| 21 | setTimeout(function() { |
| 22 | console.log("calling cb 2"); |
| 23 | cb(); |
| 24 | }, 1000); |
| 25 | }); |
| 26 | pend.wait(function(err) { |
| 27 | console.log("this is excuted when the first 2 have returned."); |
| 28 | console.log("err is a possible error in the standard callback style."); |
| 29 | }); |
| 30 | ``` |
| 31 | |
| 32 | Output: |
| 33 | |
| 34 | ``` |
| 35 | this function is immediately executed |
| 36 | this function is also immediately executed |
| 37 | calling cb 1 |
| 38 | calling cb 2 |
| 39 | this is excuted when the first 2 have returned. |
| 40 | err is a possible error in the standard callback style. |
| 41 | ``` |