Skip to main content
Module

x/fun/mod.ts>promise.abortable

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Latest
function promise.abortable
import { promise } from "https://deno.land/x/fun@v2.0.0/mod.ts";
const { abortable } = promise;

Make an existing Promise somewhat abortable. While the returned promise does resolve when the abort signal occurs, the existing promise continues running in the background. For this reason it is important to catch any errors associated with the original promise and to not implement side effects in the aborted promise.

Examples

Example 1

import { abortable, wait } from "./promise.ts";
import { pipe } from "./fn.ts";

const controller = new AbortController();
const slow = wait(1000).then(() => 1);
const wrapped = pipe(
  slow,
  abortable(controller.signal, msg => msg),
);

setTimeout(() => controller.abort("Hi"), 500);

// After 500ms result contains the following
// { tag: "Left", left: "Hi" }
const result = await wrapped;

Parameters

signal: AbortSignal
onAbort: (reason: unknown) => B

Returns

<A>(ua: Promise<A>) => Promise<Either<B, A>>