Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/ayonli_jsext/async.ts>abortable

A JavaScript extension package for building strong and modern applications.
Latest
function abortable
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

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

Returns

AsyncIterable<T>

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

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

Returns

Promise<T>