Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/dtils/deps.ts>asyncUtils.abortableAsyncIterable

The best unofficial library of utilities for Deno applications
Go to Latest
function asyncUtils.abortableAsyncIterable
import { asyncUtils } from "https://deno.land/x/dtils@2.5.0/deps.ts";
const { abortableAsyncIterable } = asyncUtils;

Make AsyncIterable abortable with the given signal.

Examples

Example 1

import { abortableAsyncIterable } from "https://deno.land/std@0.224.0/async/mod.ts";
import { delay } from "https://deno.land/std@0.224.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 abortableAsyncIterable(p(), c.signal)) {
  items.push(item);
}

Parameters

p: AsyncIterable<T>
signal: AbortSignal

Returns

AsyncGenerator<T>