Created
June 25, 2015 23:26
-
-
Save nikhedonia/ea8ee9fc503cfcb98479 to your computer and use it in GitHub Desktop.
async_generator example
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 Promise = require('promise'); | |
function* counter(){ | |
let i=0; | |
while(1){ | |
yield i++; | |
} | |
} | |
function wait(time){ | |
return new Promise(function(done){ | |
setTimeout(done,time); | |
}); | |
} | |
async function* delay(){ | |
for(const x of this){ | |
await wait(1000); | |
yield x; | |
} | |
} | |
async function logAsyncCount1(){ | |
var x = delay.call(counter()); | |
console.log('start'); | |
while(1){ | |
var obj=await x.next(); | |
if(obj.done) return; | |
console.log(obj.value); | |
} | |
console.log('end'); | |
} | |
async function logAsyncCount2(){ | |
var it = delay.call(counter()); | |
console.log('start'); | |
for(var y of it()){ | |
console.log(y); | |
} | |
console.log('end'); | |
} | |
logAsyncCount1(); |
Author
nikhedonia
commented
Jun 25, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment