Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadnazzar/9708a8c8a35dc7f4db4e76e2f7d07f87 to your computer and use it in GitHub Desktop.
Save hadnazzar/9708a8c8a35dc7f4db4e76e2f7d07f87 to your computer and use it in GitHub Desktop.
const promiseWithMilliseconds = (ms, index) => {
return new Promise((resolve,reject) => {
console.time(`Time:${index}`)
setTimeout(() => {
console.timeEnd(`Time:${index}`)
return resolve(`Resolved in ${ms}`)
}, ms);
})
.catch((error) => console.log(error))
}
const promiseWithMillisecondsToFail = (ms,index) => {
return new Promise((resolve,reject) => {
setTimeout(() => {
return reject(`Failed promise after: ${ms} ms`)
}, ms);
})
.catch((error) => console.log(error))
}
Promise.all([promiseWithMillisecondsToFail(1000,1), promiseWithMilliseconds(2000,2), promiseWithMilliseconds(3000,3)])
.then((result) => {
console.log('Result', result)
})
.catch((error) => console.log(`Catch block, Error: ${error}`))
/*
Failed promise after: 1000 ms
Time:2: 2001.328857421875 ms
Time:2: 2.002s
Time:3: 3002.52587890625 ms
Time:3: 3.003s
Result (3) [undefined, 'Resolved in 2000', 'Resolved in 3000']
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment