Created
June 21, 2022 00:03
-
-
Save hadnazzar/9708a8c8a35dc7f4db4e76e2f7d07f87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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