Last active
March 20, 2021 12:25
-
-
Save ashwinkumar2438/497db7d546821e9028158f317c7052d6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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