UNPKG

1.35 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.createAsyncIterator = void 0;
4const iterall_1 = require("iterall");
5const createAsyncIterator = async (lazyFactory, filterFn) => {
6 const asyncIterator = await lazyFactory;
7 const getNextValue = async () => {
8 if (!asyncIterator || typeof asyncIterator.next !== 'function') {
9 return Promise.reject(asyncIterator);
10 }
11 const payload = await asyncIterator.next();
12 if (payload.done === true) {
13 return payload;
14 }
15 return Promise.resolve(filterFn(payload.value))
16 .catch(() => false)
17 .then((result) => (result ? payload : getNextValue()));
18 };
19 return {
20 next() {
21 return getNextValue();
22 },
23 return() {
24 const isAsyncIterator = asyncIterator && typeof asyncIterator.return === 'function';
25 return isAsyncIterator
26 ? asyncIterator.return()
27 : Promise.resolve({
28 done: true,
29 value: asyncIterator,
30 });
31 },
32 throw(error) {
33 return asyncIterator.throw(error);
34 },
35 [iterall_1.$$asyncIterator]() {
36 return this;
37 },
38 };
39};
40exports.createAsyncIterator = createAsyncIterator;