Skip to main content
Deno 2 is finally here 🎉️
Learn more

Abortable promises for Deno

Promises with an abort() method and a cleanup function.

Install

deno cache -r https://deno.land/x/abortable/mod.ts

Test

Catch

deno run https://deno.land/x/abortable/test/catch.ts

Race

deno run https://deno.land/x/abortable/test/race.ts

Simple usage

import { Abortable } from "https://deno.land/x/abortable/mod.ts"

const timeout = Abortable.create((ok, err) => {
  const id = setTimeout(ok, 1000)
  return () => clearTimeout(id) // Abort function
})

timeout.abort() // Call the abort function and reject with AbortSignal

try{
  await timeout // Will throw AbortSignal because it has been aborted
} catch(e){
  if(e instanceof AbortSignal) // Check if it has been aborted
    // ...
}

Usage with fetch

import { Abort, Abortable } from "https://deno.land/x/abortable/mod.ts"

const request = Abort.fetch("...", { ... })
request.abort() // Will abort the request

Usage with race

import { Abort, Abortable } from "https://deno.land/x/abortable/mod.ts"

const first = Abortable.create((ok, err) => {
  const id = setTimeout(ok, 1000)
  return () => clearTimeout(id) // Abort function
})

const second = Abortable.create((ok, err) => {
  const id = setTimeout(ok, 2000)
  return () => clearTimeout(id) // Abort function
})

// Wait for the first promise to settle
// Then call the abort function of the other
await Abort.race([first, second])