Last active
March 30, 2023 04:29
-
-
Save victorquinn/8030190 to your computer and use it in GitHub Desktop.
Promise "loop" using the Bluebird library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; | |
// And below is a sample usage of this promiseWhile function | |
var sum = 0, | |
stop = 10; | |
promiseWhile(function() { | |
// Condition for stopping | |
return sum < stop; | |
}, function() { | |
// The function to run, should return a promise | |
return new Promise(function(resolve, reject) { | |
// Arbitrary 250ms async method to simulate async process | |
setTimeout(function() { | |
sum++; | |
// Print out the sum thus far to show progress | |
console.log(sum); | |
resolve(); | |
}, 250); | |
}); | |
}).then(function() { | |
// Notice we can chain it because it's a Promise, this will run after completion of the promiseWhile Promise! | |
console.log("Done"); | |
}); |
@mkbitz, won't your solution run out of memory unless the JS engine supports tail call optimization?
@petkaantonov your version crashes in chrome with RangeError: Maximum call stack size exceeded.
Still looking for and elegant alternative for bluebird that does actually work...
ES6 native implementation
var promiseWhile = function(condition, action) {
var resolver = Promise.defer();
var loop = function() {
if (!condition()) return resolver.resolve();
return new Promise(action)
.then(loop)
.catch(resolver.reject);
};
process.nextTick(loop);
return resolver.promise;
};
var sum = 0, stop = 10;
promiseWhile(function() {
return sum < stop;
},function(resolve,reject){
setTimeout(function() {
sum++;
console.log(sum);
resolve();
}, 250);
}).then(function() {
console.log("Done");
});
For loop in an array
promiseFor = Promise.method(function(arr,action,steps) {
"use strict";
if(!steps) steps = 0;
if(arr.length<=steps) return;
return new Promise((resolve,reject)=>{
try {
action(steps);
resolve();
}
catch(e) {
reject(e);
}
}).then(Promise.for.bind(null,arr,action,steps+1));
});
Usage
asyncFunc()
.then(()=>{
var arr = [1,2,3]
return new promiseFor(arr,(i)=>{ console.log(i) });
})
.then(()=>{
console.log("done iterating");
});
Doing async function on each members of the array
global.Promise.asyncOnEach = Promise.method(function(arr,action,steps) {
if(!steps) steps = 0;
console.log("Currently working on index:",steps,"Value:",arr[steps]);
if(arr.length<=steps) {
console.log("Finished working on the array, now will return it");
console.log(arr);
return arr;
}
return new Promise((resolve,reject)=>{
action(arr[steps]).then((v)=>{
arr[steps] = v;
console.log(v);
resolve(arr);
}).catch((e)=>{
reject(e);
});
}).then(Promise.asyncOnEach.bind(null,arr,action,steps+1));
});
Usage:
Promise.asyncOnEach([3,6,9],asyncFunc)
.then((arr)=>{
console.log("The result is:",arr);
res.send("OK");
}).catch((e)=>{
console.log(e);
res.send("error");
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For anyone who doesn't want to use
process.nextTick()
, I've written my own solution here. I'm using the Q framework, but it could be very easily reworked to use Bluebird. It returns a promise which will resolve to the final iteration's return value, so it works a lot likereduce
.That is the promise-y equivalent of this function:
Usage is straightforward: