Last active
February 8, 2022 07:40
-
-
Save iMichaelOwolabi/00b0ef1498b10d81f2e1039389a0b65c to your computer and use it in GitHub Desktop.
Sample use of non-blocking promise implementation in Node.js
This file contains hidden or 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
| // Do this instead because unrelated awaits doesn't block each other ✅ | |
| const nonBlockinAsnycFunction = async () => { | |
| // Start recording the execution time of the following code. | |
| console.time('non-blocking-await'); | |
| const promise1 = samplePromise1(); | |
| const promise2 = samplePromise2(); | |
| // Use promise.all to ensure the two promises are executed in parallel. | |
| const [promiseResult1, promiseResult2, promiseResult3] = await Promise.all([promise1, promise2, promise3]); | |
| console.log(promiseResult1); | |
| console.log(promiseResult2); | |
| // Stop recording the execution time and log the elapsed time to the console. | |
| console.timeEnd('non-blocking-await'); | |
| }; | |
| nonBlockinAsnycFunction(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment