|
| 1 | +// Copyright 2018-2026 the Deno authors. MIT license. |
| 2 | +// This module is browser compatible. |
| 3 | + |
| 4 | +/** Options for {@linkcode abortable}. */ |
| 5 | +export interface AbortableOptions { |
| 6 | + /** The signal to abort the promise with. */ |
| 7 | + signal?: AbortSignal | undefined; |
| 8 | +} |
| 9 | + |
| 10 | +// TODO(iuioiua): Remove `ignore` directives from following snippets |
| 11 | +/** |
| 12 | + * Make a {@linkcode Promise} abortable with the given signal. |
| 13 | + * |
| 14 | + * @throws {DOMException} If the signal is already aborted and `signal.reason` |
| 15 | + * is undefined. Otherwise, throws `signal.reason`. |
| 16 | + * @typeParam T The type of the provided and returned promise. |
| 17 | + * @param p The promise to make abortable. |
| 18 | + * @param signal The signal to abort the promise with. |
| 19 | + * @returns A promise that can be aborted. |
| 20 | + * |
| 21 | + * @example Error-handling a timeout |
| 22 | + * ```ts ignore |
| 23 | + * import { abortable, delay } from "@std/async"; |
| 24 | + * import { assertRejects, assertEquals } from "@std/assert"; |
| 25 | + * |
| 26 | + * const promise = delay(1_000); |
| 27 | + * |
| 28 | + * // Rejects with `DOMException` after 100 ms |
| 29 | + * await assertRejects( |
| 30 | + * () => abortable(promise, AbortSignal.timeout(100)), |
| 31 | + * DOMException, |
| 32 | + * "Signal timed out." |
| 33 | + * ); |
| 34 | + * ``` |
| 35 | + * |
| 36 | + * @example Error-handling an abort |
| 37 | + * ```ts ignore |
| 38 | + * import { abortable, delay } from "@std/async"; |
| 39 | + * import { assertRejects, assertEquals } from "@std/assert"; |
| 40 | + * |
| 41 | + * const promise = delay(1_000); |
| 42 | + * const controller = new AbortController(); |
| 43 | + * controller.abort(new Error("This is my reason")); |
| 44 | + * |
| 45 | + * // Rejects with `DOMException` immediately |
| 46 | + * await assertRejects( |
| 47 | + * () => abortable(promise, controller.signal), |
| 48 | + * Error, |
| 49 | + * "This is my reason" |
| 50 | + * ); |
| 51 | + * ``` |
| 52 | + */ |
| 53 | +export function abortable<T>( |
| 54 | + p: Promise<T>, |
| 55 | + signal: AbortSignal | AbortableOptions, |
| 56 | +): Promise<T>; |
| 57 | +/** |
| 58 | + * Make an {@linkcode AsyncIterable} abortable with the given signal. |
| 59 | + * |
| 60 | + * @throws {DOMException} If the signal is already aborted and `signal.reason` |
| 61 | + * is undefined. Otherwise, throws `signal.reason`. |
| 62 | + * @typeParam T The type of the provided and returned async iterable. |
| 63 | + * @param p The async iterable to make abortable. |
| 64 | + * @param signal The signal to abort the promise with. |
| 65 | + * @returns An async iterable that can be aborted. |
| 66 | + * |
| 67 | + * @example Error-handling a timeout |
| 68 | + * ```ts |
| 69 | + * import { abortable, delay } from "@std/async"; |
| 70 | + * import { assertRejects, assertEquals } from "@std/assert"; |
| 71 | + * |
| 72 | + * const asyncIter = async function* () { |
| 73 | + * yield "Hello"; |
| 74 | + * await delay(1_000); |
| 75 | + * yield "World"; |
| 76 | + * }; |
| 77 | + * |
| 78 | + * const items: string[] = []; |
| 79 | + * // Below throws `DOMException` after 100 ms and items become `["Hello"]` |
| 80 | + * await assertRejects( |
| 81 | + * async () => { |
| 82 | + * for await (const item of abortable(asyncIter(), AbortSignal.timeout(100))) { |
| 83 | + * items.push(item); |
| 84 | + * } |
| 85 | + * }, |
| 86 | + * DOMException, |
| 87 | + * "Signal timed out." |
| 88 | + * ); |
| 89 | + * assertEquals(items, ["Hello"]); |
| 90 | + * ``` |
| 91 | + * |
| 92 | + * @example Error-handling an abort |
| 93 | + * ```ts |
| 94 | + * import { abortable, delay } from "@std/async"; |
| 95 | + * import { assertRejects, assertEquals } from "@std/assert"; |
| 96 | + * |
| 97 | + * const asyncIter = async function* () { |
| 98 | + * yield "Hello"; |
| 99 | + * await delay(1_000); |
| 100 | + * yield "World"; |
| 101 | + * }; |
| 102 | + * const controller = new AbortController(); |
| 103 | + * controller.abort(new Error("This is my reason")); |
| 104 | + * |
| 105 | + * const items: string[] = []; |
| 106 | + * // Below throws `DOMException` immediately |
| 107 | + * await assertRejects( |
| 108 | + * async () => { |
| 109 | + * for await (const item of abortable(asyncIter(), controller.signal)) { |
| 110 | + * items.push(item); |
| 111 | + * } |
| 112 | + * }, |
| 113 | + * Error, |
| 114 | + * "This is my reason" |
| 115 | + * ); |
| 116 | + * assertEquals(items, []); |
| 117 | + * ``` |
| 118 | + */ |
| 119 | + |
| 120 | +export function abortable<T>( |
| 121 | + p: AsyncIterable<T>, |
| 122 | + signal: AbortSignal | AbortableOptions, |
| 123 | +): AsyncGenerator<T>; |
| 124 | +export function abortable<T>( |
| 125 | + p: Promise<T> | AsyncIterable<T>, |
| 126 | + signal: AbortSignal | AbortableOptions, |
| 127 | +): Promise<T> | AsyncIterable<T> { |
| 128 | + if (!(signal instanceof AbortSignal)) { |
| 129 | + if (!signal.signal) { |
| 130 | + return p; |
| 131 | + } |
| 132 | + signal = signal.signal; |
| 133 | + } |
| 134 | + if (p instanceof Promise) { |
| 135 | + return abortablePromise(p, signal); |
| 136 | + } else { |
| 137 | + return abortableAsyncIterable(p, signal); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +function abortablePromise<T>( |
| 142 | + p: Promise<T>, |
| 143 | + signal: AbortSignal, |
| 144 | +): Promise<T> { |
| 145 | + const { promise, reject } = Promise.withResolvers<never>(); |
| 146 | + const abort = () => reject(signal.reason); |
| 147 | + if (signal.aborted) abort(); |
| 148 | + signal.addEventListener("abort", abort, { once: true }); |
| 149 | + return Promise.race([promise, p]).finally(() => { |
| 150 | + signal.removeEventListener("abort", abort); |
| 151 | + }); |
| 152 | +} |
| 153 | + |
| 154 | +async function* abortableAsyncIterable<T>( |
| 155 | + p: AsyncIterable<T>, |
| 156 | + signal: AbortSignal, |
| 157 | +): AsyncGenerator<T> { |
| 158 | + signal.throwIfAborted(); |
| 159 | + const { promise, reject } = Promise.withResolvers<never>(); |
| 160 | + const abort = () => reject(signal.reason); |
| 161 | + signal.addEventListener("abort", abort, { once: true }); |
| 162 | + |
| 163 | + const it = p[Symbol.asyncIterator](); |
| 164 | + try { |
| 165 | + while (true) { |
| 166 | + const race = Promise.race([promise, it.next()]); |
| 167 | + race.catch(() => { |
| 168 | + signal.removeEventListener("abort", abort); |
| 169 | + }); |
| 170 | + const { done, value } = await race; |
| 171 | + if (done) { |
| 172 | + signal.removeEventListener("abort", abort); |
| 173 | + const result = await it.return?.(value); |
| 174 | + return result?.value; |
| 175 | + } |
| 176 | + yield value; |
| 177 | + } |
| 178 | + } catch (e) { |
| 179 | + await it.return?.(); |
| 180 | + throw e; |
| 181 | + } |
| 182 | +} |
0 commit comments