Skip to content

Instantly share code, notes, and snippets.

@LearningNerd
Created October 9, 2018 21:40
Show Gist options
  • Save LearningNerd/0c6b077ea702f4ecbf72291ba0f82cd7 to your computer and use it in GitHub Desktop.
Save LearningNerd/0c6b077ea702f4ecbf72291ba0f82cd7 to your computer and use it in GitHub Desktop.
Just a quick example of a generator function, mostly borrowed from https://davidwalsh.name/async-generators
// Just a quick example of a generator function,
// mostly borrowed from https://davidwalsh.name/async-generators
function request(param) {
console.log("called request() with " + param);
makeAsyncCall( param, function(response){
it.next( response );
} );
}
function makeAsyncCall(param,cb) {
console.log("called makeAsyncCall()");
setTimeout(function(){
console.log("async thingy done!!!");
cb(param);
}, 3000);
}
function *main() {
let result1 = yield request( "param 1" );
let result2 = yield request( "param 2 plus " + result1 );
console.log( "Value of final result: " + result2 );
}
let it = main();
it.next();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment