Generator with return statement skips the execution of for-await-of loop.
(async () => {
const gen = async function* () {
return { newState: "FAILURE" };
};
for await (const { newState } of gen()) {
console.log("Yey! new state is:", newState);
// do other stuff
}
})();
In the case written above, the whole for-await-of construction will never console.log anything. But when you swap return for yield (yield { newState: 'FAILURE' }) everything work as intended.
(async () => {
const gen = async function* () {
yield { newState: "FAILURE" };
};
for await (const { newState } of gen()) {
console.log("Yey! new state is:", newState);
// do other stuff
}
})();
WHY?