Skip to content

Instantly share code, notes, and snippets.

@mfbx9da4
Last active August 9, 2021 11:27
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 mfbx9da4/cf955f794950eb4dc11743472a5e62b9 to your computer and use it in GitHub Desktop.
Save mfbx9da4/cf955f794950eb4dc11743472a5e62b9 to your computer and use it in GitHub Desktop.
Makes abort signals awaitable
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