Skip to content

Instantly share code, notes, and snippets.

@benjamingr
Created September 13, 2021 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjamingr/f0b0871d845e7bfbecc392f978de58bb to your computer and use it in GitHub Desktop.
Save benjamingr/f0b0871d845e7bfbecc392f978de58bb to your computer and use it in GitHub Desktop.
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
async function* delayedRange() {
try {
for(let i = 1; i <= 1000; i++) {
await delay(100);
yield i;
}
} catch (e) {
console.log("Oh no error in iterator", e);
} finally {
console.log("OMG, finally blocks run on termination");
}
}
const range = delayedRange();
console.log(await range.next()); // { value: 1, done: false }
console.log(await range.next()); // { value 2, done: false }
// range.return(); // this runs the `finally` block, logs and finishes the generator.
range.throw(new RangeError('fun')); // this runs *both* the catch and the finally
// In a for await loop, `break`ing the loop calls `.return` implicitly so loops support cancellation
// In a for await loop, `throw`ing similarly propagates the error - funnily it's just this way because
// cancellation progress stopped "mid way" and because of the duality proposal.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment