I use JS generator to yield a value in a callback of setTimeout
:
function* sleep() {
// Using yield here is OK
// yield 5;
setTimeout(function() {
// Using yield here will throw error
yield 5;
}, 5000);
}
// sync
const sleepTime = sleep().next()
Why I can't yield values inside a callback in the generator?
function*
declaration is synchronous. You can yield a newPromise
object, chain.then()
to.next().value
to retrieve resolvedPromise
value