Last active
August 9, 2021 11:27
-
-
Save mfbx9da4/cf955f794950eb4dc11743472a5e62b9 to your computer and use it in GitHub Desktop.
Makes abort signals awaitable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class AbortSignal { | |
public pending = false | |
public completed = false | |
public onAbort: () => void | undefined | |
private resolve: () => void | undefined | |
async abort() { | |
if (this.completed) return | |
const promise = new Promise<void>((r) => { | |
this.resolve = r | |
}) | |
this.pending = true | |
this.onAbort?.() | |
await promise | |
} | |
done() { | |
if (this.completed) throw new Error('Already aborted') | |
if (this.pending) this.resolve() | |
this.completed = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment