Skip to content

Instantly share code, notes, and snippets.

@ashwinkumar2438
Last active March 20, 2021 12:25
Show Gist options
  • Save ashwinkumar2438/497db7d546821e9028158f317c7052d6 to your computer and use it in GitHub Desktop.
Save ashwinkumar2438/497db7d546821e9028158f317c7052d6 to your computer and use it in GitHub Desktop.
function* asyncgen(userinput){
let res= yield userinput;
console.log(res); //print final result;
}
/** doasync() used to fake async task.
* @param: gen {function} generator function as input to be processed.
* @param: val {string} data to be passed.
*/
let doasync=(gen,val)=>{
var getdata=gen(val);
et data= getdata.next();
setTimeout(()=>{
if(data.value.includes('get'))getdata.next(data)
else getdata.return(); //cancel all yields and process return and execute finally block if present.
},3000);
}
doasync(asyncgen,'get-data'); // @returns {value: "get-data", done: false} after 3 seconds.
doasync(asyncgen,'cancel-data'); // @returns undefined as generator is returned.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment