deno.land / std@0.180.0 / async / abortable.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.import { deferred } from "./deferred.ts";
/** * Make Promise abortable with the given signal. * * @example * ```typescript * import { abortable } from "https://deno.land/std@$STD_VERSION/async/mod.ts"; * import { delay } from "https://deno.land/std@$STD_VERSION/async/mod.ts"; * * const p = delay(1000); * const c = new AbortController(); * setTimeout(() => c.abort(), 100); * * // Below throws `DOMException` after 100 ms * await abortable(p, c.signal); * ``` */export function abortable<T>(p: Promise<T>, signal: AbortSignal): Promise<T>;/** * Make AsyncIterable abortable with the given signal. * * @example * ```typescript * import { abortable } from "https://deno.land/std@$STD_VERSION/async/mod.ts"; * import { delay } from "https://deno.land/std@$STD_VERSION/async/mod.ts"; * * const p = async function* () { * yield "Hello"; * await delay(1000); * yield "World"; * }; * const c = new AbortController(); * setTimeout(() => c.abort(), 100); * * // Below throws `DOMException` after 100 ms * // and items become `["Hello"]` * const items: string[] = []; * for await (const item of abortable(p(), c.signal)) { * items.push(item); * } * ``` */export function abortable<T>( p: AsyncIterable<T>, signal: AbortSignal,): AsyncGenerator<T>;export function abortable<T>( p: Promise<T> | AsyncIterable<T>, signal: AbortSignal,): Promise<T> | AsyncIterable<T> { if (p instanceof Promise) { return abortablePromise(p, signal); } else { return abortableAsyncIterable(p, signal); }}
/** * Make Promise abortable with the given signal. * * @example * ```typescript * import { abortablePromise } from "https://deno.land/std@$STD_VERSION/async/mod.ts"; * * const request = fetch("https://example.com"); * * const c = new AbortController(); * setTimeout(() => c.abort(), 100); * * const p = abortablePromise(request, c.signal); * * // The below throws if the request didn't resolve in 100ms * await p; * ``` */export function abortablePromise<T>( p: Promise<T>, signal: AbortSignal,): Promise<T> { if (signal.aborted) { return Promise.reject(createAbortError(signal.reason)); } const waiter = deferred<never>(); const abort = () => waiter.reject(createAbortError(signal.reason)); signal.addEventListener("abort", abort, { once: true }); return Promise.race([ waiter, p.finally(() => { signal.removeEventListener("abort", abort); }), ]);}
/** * Make AsyncIterable abortable with the given signal. * * @example * ```typescript * import { abortableAsyncIterable } from "https://deno.land/std@$STD_VERSION/async/mod.ts"; * import { delay } from "https://deno.land/std@$STD_VERSION/async/mod.ts"; * * const p = async function* () { * yield "Hello"; * await delay(1000); * yield "World"; * }; * const c = new AbortController(); * setTimeout(() => c.abort(), 100); * * // Below throws `DOMException` after 100 ms * // and items become `["Hello"]` * const items: string[] = []; * for await (const item of abortableAsyncIterable(p(), c.signal)) { * items.push(item); * } * ``` */export async function* abortableAsyncIterable<T>( p: AsyncIterable<T>, signal: AbortSignal,): AsyncGenerator<T> { if (signal.aborted) { throw createAbortError(signal.reason); } const waiter = deferred<never>(); const abort = () => waiter.reject(createAbortError(signal.reason)); signal.addEventListener("abort", abort, { once: true });
const it = p[Symbol.asyncIterator](); while (true) { const { done, value } = await Promise.race([waiter, it.next()]); if (done) { signal.removeEventListener("abort", abort); return; } yield value; }}
// This `reason` comes from `AbortSignal` thus must be `any`.// deno-lint-ignore no-explicit-anyfunction createAbortError(reason?: any): DOMException { return new DOMException( reason ? `Aborted: ${reason}` : "Aborted", "AbortError", );}
Version Info