Skip to content

Instantly share code, notes, and snippets.

@nikhedonia
Created June 25, 2015 23:26
Show Gist options
  • Save nikhedonia/ea8ee9fc503cfcb98479 to your computer and use it in GitHub Desktop.
Save nikhedonia/ea8ee9fc503cfcb98479 to your computer and use it in GitHub Desktop.
async_generator example
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();
@nikhedonia
Copy link
Author

npm install promise
npm install -g regenerator
regenerator -r async_generator.js | node

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment