abortable
import { abortable } from "https://github.gh-proxy.cn/raw.githubusercontent.com/baetheus/fun/main/promise.ts";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.
@example
import { abortable, wait } from "./promise.ts";
import { pipe } from "./fn.ts";
const controller = new AbortController();
const slow = wait(100).then(() => 1);
const wrapped = pipe(
slow,
abortable(controller.signal, msg => msg),
);
setTimeout(() => controller.abort("Hi"), 200);
// After 200ms result contains the following
// { tag: "Left", left: "Hi" }
const result = await wrapped;
await wait(200) // We wait to not leak async ops
function abortable<B>(signal: AbortSignal, onAbort: (reason: unknown) => B): <A>(ua: Promise<A>) => Promise<Either<B, A>>;