UNPKG

graphql

Version:

A Query Language and Runtime which can target any service.

32 lines 1.07 kB
import { isPromise } from "../jsutils/isPromise.mjs"; import { withConcurrentAbruptClose } from "./withConcurrentAbruptClose.mjs"; export function mapAsyncIterable(iterable, callback) { const iterator = iterable[Symbol.asyncIterator](); const returnFn = iterator.return?.bind(iterator); const throwFn = iterator.throw?.bind(iterator); const onReturn = returnFn ? () => callIgnoringErrors(returnFn) : () => Promise.resolve(); const onThrow = throwFn ? (reason) => callIgnoringErrors(() => throwFn(reason)) : onReturn; return withConcurrentAbruptClose(mapAsyncIterableImpl(iterable, callback), onReturn, onThrow); } async function callIgnoringErrors(fn) { try { await fn(); } catch { } } async function* mapAsyncIterableImpl(iterable, mapFn) { for await (const value of iterable) { const result = mapFn(value); if (isPromise(result)) { yield await result; continue; } yield result; } } //# sourceMappingURL=mapAsyncIterable.js.map