import { abortable } from "https://deno.land/x/ayonli_jsext@v0.9.72/async.ts";
Wraps an async iterable object with an abort signal.
Examples
Example 1
Example 1
import { abortable, sleep } from "@ayonli/jsext/async";
async function* generate() {
yield "Hello";
await sleep(1000);
yield "World";
}
const iterator = generate();
const controller = new AbortController();
setTimeout(() => controller.abort(), 100);
// prints "Hello" and throws AbortError after 100ms
for await (const value of abortable(iterator, controller.signal)) {
console.log(value); // "Hello"
}
Parameters
task: AsyncIterable<T>
signal: AbortSignal
Try to resolve a promise with an abort signal.
NOTE: This function does not cancel the task itself, it only prematurely breaks the current routine when the signal is aborted. In order to support cancellation, the task must be designed to handle the abort signal itself.
Examples
Example 1
Example 1
import { abortable, sleep } from "@ayonli/jsext/async";
const task = sleep(1000);
const controller = new AbortController();
setTimeout(() => controller.abort(), 100);
await abortable(task, controller.signal); // throws AbortError after 100ms
Parameters
task: PromiseLike<T>
signal: AbortSignal