Skip to content

Instantly share code, notes, and snippets.

@nijotz
Created July 24, 2019 19:51
Show Gist options
  • Save nijotz/3834d6d0a85653fe20a85f7039e5544c to your computer and use it in GitHub Desktop.
Save nijotz/3834d6d0a85653fe20a85f7039e5544c to your computer and use it in GitHub Desktop.
function sleepReject(time, msg) {
return new Promise((resolve, reject) => {
setTimeout(() => reject(msg || 'reject'), time);
});
}
async function test() {
try {
let valuePromises1 = [1000, 2000, 3000].map(x => sleepReject(x));
let valuePromises2 = [4000, 5000].map(x => sleepReject(x));
let [values1, values2] = await Promise.all([Promise.all(valuePromises1), Promise.all(valuePromises2)]);
console.log(values1);
console.log(values2);
} catch (error) {
console.log(error);
}
}
test()
// node promises.js
// "reject"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment