Skip to content

Instantly share code, notes, and snippets.

@nijotz nijotz/promises.js
Last active Jul 24, 2019

Embed
What would you like to do?
// cleaner output, but same results without
const Promise = require('bluebird');
// What is the best way to run async functions
// * in parallel
// * fail fast
// * not have unhandled promises
// * single failure path (try/catch)
// ?
function sleepReject(time, msg) {
return new Promise((resolve, reject) => {
console.log(`Running ${msg}`);
setTimeout(() => {
if (process.argv[3] === 'resolve') {
resolve(`${msg} resolve`);
} else {
reject(`${msg} reject`);
}
}, time);
});
}
// Unhandled promise rejection for the '1000 reject' promise
async function test1() {
try {
let valuePromise1 = sleepReject(2000, '2000');
let valuePromise2 = sleepReject(1000, '1000');
let value1 = await valuePromise1;
let value2 = await valuePromise2;
console.log(value1);
console.log(value2);
} catch (error) {
console.log('Error', error);
}
}
// No unhandled promise rejections but '1000 reject' is never run (no parallel)
async function test2() {
try {
let value1 = await sleepReject(2000, '2000');
let value2 = await sleepReject(1000, '1000');
console.log(value1);
console.log(value2);
} catch (error) {
console.log('Error', error);
}
}
// No UPRs, fails fast, runs in parallel
async function test3() {
let valuePromise1 = sleepReject(2000, '2000');
let valuePromise2 = sleepReject(1000, '1000');
try {
let values = await Promise.all([valuePromise1, valuePromise2]);
values.forEach(x => console.log(x));
} catch (error) {
console.log('Error', error);
}
}
// UPRs on 4000 and 5000
async function test4() {
let valuePromises1 = [1000, 2000, 3000].map(x => sleepReject(x, `${x}`));
let valuePromises2 = [4000, 5000].map(x => sleepReject(x, `${x}`));
try {
let values1 = await Promise.all(valuePromises1);
let values2 = await Promise.all(valuePromises2);
values1.forEach(x => console.log(x));
values2.forEach(x => console.log(x));
} catch (error) {
console.log('Error', error);
}
}
// No UPRs, fails fast, runs in parallel
async function test5() {
let valuePromises1 = [1000, 2000, 3000].map(x => sleepReject(x, `${x}`));
let valuePromises2 = [4000, 5000].map(x => sleepReject(x, `${x}`));
try {
let [values1, values2] = await Promise.all([Promise.all(valuePromises1), Promise.all(valuePromises2)]);
values1.forEach(x => console.log(x));
values2.forEach(x => console.log(x));
} catch (error) {
console.log('Error', error);
}
}
switch(process.argv[2]) {
case 'test1':
test1();
return;
case 'test2':
test2();
return;
case 'test3':
test3();
return;
case 'test4':
test4();
return;
case 'test5':
test5();
return;
}
// $ node promises.js test1 resolve
// Running 2000
// Running 1000
// 2000 resolve
// 1000 resolve
//
// $ node promises.js test1 reject
// Running 2000
// Running 1000
// Unhandled rejection 1000 reject
//
// Error 2000 reject
//
// $ node promises.js test2 resolve
// Running 2000
// Running 1000
// 2000 resolve
// 1000 resolve
//
// $ node promises.js test2 reject
// Running 2000
// Error 2000 reject
//
// $ node promises.js test3 resolve
// Running 2000
// Running 1000
// 2000 resolve
// 1000 resolve
//
// $ node promises.js test3 reject
// Running 2000
// Running 1000
// Error 1000 reject
//
// $ node promises.js test4 resolve
// Running 1000
// Running 2000
// Running 3000
// Running 4000
// Running 5000
// 1000 resolve
// 2000 resolve
// 3000 resolve
// 4000 resolve
// 5000 resolve
//
// $ node promises.js test4 reject
// Running 1000
// Running 2000
// Running 3000
// Running 4000
// Running 5000
// Error 1000 reject
// Unhandled rejection 4000 reject
//
// Unhandled rejection 5000 reject
//
// $ node promises.js test5 resolve
// Running 1000
// Running 2000
// Running 3000
// Running 4000
// Running 5000
// 1000 resolve
// 2000 resolve
// 3000 resolve
// 4000 resolve
// 5000 resolve
//
// $ node promises.js test5 reject
// Running 1000
// Running 2000
// Running 3000
// Running 4000
// Running 5000
// Error 1000 reject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.