functionsetTimeOutThunk(time) { return(callback) =>setTimeout(callback, time, time); }
function* generate() { let val = yieldsetTimeOutThunk(1000); console.log(val); val = yieldsetTimeOutThunk(2000); console.log(val); val = yieldsetTimeOutThunk(3000); console.log(val); val = yieldsetTimeOutThunk(4000); console.log(val); }
let gen = generate(); let { value } = gen.next(); // {value: [[callbackFunction]] , done: flase} value(() => gen.next(123)); //123 {value: [[callbackFunction]] , done: flase}
functionsetTimeOutThunk(time) { return(callback) =>setTimeout(callback, time, time); }
function* gen() { let val = yieldsetTimeOutThunk(1000); console.log(val); val = yieldsetTimeOutThunk(2000); console.log(val); val = yieldsetTimeOutThunk(3000); console.log(val); val = yieldsetTimeOutThunk(4000); console.log(val); }
functionrun(gen, ...args) { let { value, done } = gen.next(...args); if (!done) { debugger; if (value instanceofPromise) { value.then((...args) =>run(gen, ...args)); } else { value((...args) =>run(gen, ...args)); } } }