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

std/async/abortable.ts>abortable

Deno standard library
Go to Latest
The Standard Library has been moved to JSR. See the blog post for details.
function abortable
import { abortable } from "https://deno.land/std@0.202.0/async/abortable.ts";

Make Promise abortable with the given signal.

Examples

Example 1

import { abortable } from "https://deno.land/std@0.202.0/async/mod.ts";
import { delay } from "https://deno.land/std@0.202.0/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);

Parameters

p: Promise<T>
signal: AbortSignal

Returns

Promise<T>

Make AsyncIterable abortable with the given signal.

Examples

Example 1

import { abortable } from "https://deno.land/std@0.202.0/async/mod.ts";
import { delay } from "https://deno.land/std@0.202.0/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);
}

Parameters

p: AsyncIterable<T>
signal: AbortSignal

Returns

AsyncGenerator<T>