Skip to content

Instantly share code, notes, and snippets.

@phongvh
Created October 11, 2021 07:43
Show Gist options
  • Save phongvh/4f1cb7496ef8ec42073280e875b9fd43 to your computer and use it in GitHub Desktop.
Save phongvh/4f1cb7496ef8ec42073280e875b9fd43 to your computer and use it in GitHub Desktop.
Promise.all & async.parallel
import async from 'async';
const ap = () => {
async.parallel([
(cb) => {
setTimeout(() => {
console.log(2)
cb(2, 2);
}, 2000);
},
(cb) => {
setTimeout(() => {
console.log(3)
cb(null, 3);
}, 3000);
},
(cb) => {
setTimeout(() => {
console.log(1)
cb(null, 1);
}, 1000);
}
]).then((results) => {
console.log(results);
}).catch((e) => console.error(e));
}
const pall = () => {
Promise.all([
new Promise((resolve, reject) => {
setTimeout(() => {
console.log(2)
reject(2)
}, 2000);
}),
new Promise((resolve, reject) => {
setTimeout(() => {
console.log(3)
resolve(3)
}, 3000);
}),
new Promise((resolve, reject) => {
setTimeout(() => {
console.log(1)
resolve(1)
}, 1000);
})
]).then((results) => {
console.log(results);
}).catch((e) => console.error(e));
}
// ap();
pall();
// we can use multiple await, but await will return error callback only when the longest promise has been finised ( not fail fast )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment