Last active
May 16, 2017 13:04
-
-
Save nsisodiya/beb056bb5dca1c3dc817658f1ec280bb to your computer and use it in GitHub Desktop.
async await parallel
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
| $ node run1.js | |
| 1 Seconds | |
| 2 Seconds | |
| 3 Seconds | |
| 4 Seconds | |
| detail { cash: 200, online: 100 } | |
| 5 Seconds | |
| 6 Seconds | |
| 7 Seconds |
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
| $ node a.js | |
| 1 Seconds | |
| 2 Seconds | |
| 3 Seconds | |
| 4 Seconds | |
| 5 Seconds | |
| 6 Seconds | |
| 7 Seconds | |
| 8 Seconds | |
| detail { cash: 200, online: 100 } | |
| 9 Seconds | |
| 10 Seconds |
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
| async function foo(){ | |
| const p1 = getCash(); | |
| const p2 = getOnline(); | |
| const cash = await p1; | |
| const online = await p2; | |
| console.log("detail", {cash, online}); | |
| } | |
| timer(); | |
| foo(); |
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
| async function foo(){ | |
| const cash = await getCash(); | |
| const online = await getOnline(); | |
| console.log("detail", {cash, online}); | |
| } | |
| timer(); | |
| foo(); |
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
| function timer(){ | |
| var i = 1; | |
| setInterval(function(){ | |
| console.log(`${i} Seconds`); | |
| i=i+1; | |
| }, 990); | |
| } | |
| function getCash(){ | |
| return new Promise(function(resolve){ | |
| setTimeout(function(){ | |
| resolve(200); | |
| }, 4000) | |
| }); | |
| } | |
| function getOnline(){ | |
| return new Promise(function(resolve){ | |
| setTimeout(function(){ | |
| resolve(100); | |
| }, 4000) | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment