Skip to content

Instantly share code, notes, and snippets.

@nsisodiya
Last active May 16, 2017 13:04
Show Gist options
  • Select an option

  • Save nsisodiya/beb056bb5dca1c3dc817658f1ec280bb to your computer and use it in GitHub Desktop.

Select an option

Save nsisodiya/beb056bb5dca1c3dc817658f1ec280bb to your computer and use it in GitHub Desktop.
async await parallel
$ node run1.js
1 Seconds
2 Seconds
3 Seconds
4 Seconds
detail { cash: 200, online: 100 }
5 Seconds
6 Seconds
7 Seconds
$ 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
async function foo(){
const p1 = getCash();
const p2 = getOnline();
const cash = await p1;
const online = await p2;
console.log("detail", {cash, online});
}
timer();
foo();
async function foo(){
const cash = await getCash();
const online = await getOnline();
console.log("detail", {cash, online});
}
timer();
foo();
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