Skip to content

Instantly share code, notes, and snippets.

@kriskowal
Created December 4, 2020 22:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kriskowal/39e17a2d229446e1c02788997cee4f55 to your computer and use it in GitHub Desktop.
Save kriskowal/39e17a2d229446e1c02788997cee4f55 to your computer and use it in GitHub Desktop.
const streamWithContext = (context, stream) => ({
async next(value) {
return Promise.race([context.cancelled, stream.next(await value)]);
},
async return(value) {
return Promise.race([context.cancelled, stream.return(await value)]);
},
async throw(error) {
return Promise.race([context.cancelled, stream.throw(await error)]);
},
[Symbol.asyncIterator]() {
return this;
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment