J.D Nicholls
Founding Full-Stack Web3 Engineer at @baxusco | Digital nomad 🎒 | Mentor 👨🏫 | Speaker 🗣️ | Developer 👨💻 | Creator of @proyecto26 #opensource #developer
using Bluebird
var helloTimeout = function(name, timeout, cancel){
return new Promise(function(resolve, reject){
setTimeout(function(){
console.log(name);
if(cancel){
reject("cancelled");
}
else{
resolve(name);
}
}, timeout || 1000);
});
};
var users = [
{ name: "Juan", timeout: "3000", cancel: true },
{ name: "Valentina", timeout: "2000" },
{ name: "Julian", timeout: "5000", cancel: true }
];
var errors = [];
Promise.all([
helloTimeout("juan", 2000, true),
helloTimeout("valentina", 1000),
helloTimeout("julian", 3000, true)
].map(function(promise) {
return promise.catch(function(error){
errors.push(error);
}).reflect();
})).then(function(){
console.log("end");
console.log("errors:");
console.log(errors);
});
var errors = [];
Promise.map(users, function(user){
return helloTimeout(user.name, user.timeout, user.cancel).catch(function(error){
errors.push(error);
});
}).then(function(){
console.log("end");
console.log("errors:");
console.log(errors);
});
Also with .map you can define concurrency! :o
Promise.map(users, function(user){
}, {concurrency: 2});
Promise.mapSeries(users, function(user){
return helloTimeout(user.name, user.timeout, user.cancel);
}).catch(function(err){
console.log(err);
});
var users = {
user1: {},
user2: {},
user3: {}
};
Promise.map(Object.keys(users), function(key){
return User.create(users[key]);
});
/*
* @param {function(error, result)} cb - A callback to run.
*/
function foo(x, y, cb) {
return new Promise((resolve, reject) => {
request(..., (err, data) => {
if(err) {
if(cb) { cb(err); }
reject(err);
}
else{
if(cb) { cb(null, result); }
resolve(data);
}
})
});
}
By J.D Nicholls
Founding Full-Stack Web3 Engineer at @baxusco | Digital nomad 🎒 | Mentor 👨🏫 | Speaker 🗣️ | Developer 👨💻 | Creator of @proyecto26 #opensource #developer