Last active
December 30, 2016 19:01
-
-
Save hsingh23/646c0c1f09588dc38ce4b0c862144053 to your computer and use it in GitHub Desktop.
async await example sync example (node only)
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
var async = require('asyncawait/async'); | |
var await = require('asyncawait/await'); | |
console.log("starting") | |
var getSomethingAsync = function(i){ | |
return new Promise((resolve, reject) => { | |
setTimeout(()=>resolve(`money = ${i}`), Math.random()*500) | |
}) | |
} | |
var foo = async(function() { | |
var prev = null, now = 0 | |
for (let i = 0; i < 10; i ++) { | |
now = await(getSomethingAsync(i)) | |
console.log("now:", now, "prev", prev) | |
prev = now | |
} | |
console.log("done") | |
}); | |
// Batched | |
// var foo = async(function() { | |
// var prev = null, now = 0 | |
// for (let i = 0; i < 100; i += 5) { | |
// now = await(Promise.all([getSomethingAsync(i),getSomethingAsync(i+1),getSomethingAsync(i+2),getSomethingAsync(i+3),getSomethingAsync(i+4)])) | |
// console.log("now:", now, "prev", prev) | |
// prev = now | |
// } | |
// console.log("done") | |
// }); | |
foo().then(()=>{ | |
console.log("done 2") | |
}) | |
console.log("called after starting - async baby") | |
// while(1){ | |
// if (Math.random() > .9) break | |
// console.log('hi') | |
// } |
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
{ | |
"name": "asyncawait", | |
"main": "index.js", | |
"dependencies": { | |
"asyncawait": "^1.0.6" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment