UNPKG

1.7 kBJavaScriptView Raw
1import { AsyncIterableX } from './asynciterablex';
2import { returnAsyncIterator } from '../util/returniterator';
3export class CatchAllAsyncIterable extends AsyncIterableX {
4 constructor(source) {
5 super();
6 this._source = source;
7 }
8 async *[Symbol.asyncIterator]() {
9 let error = null;
10 let hasError = false;
11 for (const source of this._source) {
12 const it = source[Symbol.asyncIterator]();
13 error = null;
14 hasError = false;
15 while (1) {
16 let c = {};
17 try {
18 const { done, value } = await it.next();
19 if (done) {
20 await returnAsyncIterator(it);
21 break;
22 }
23 c = value;
24 }
25 catch (e) {
26 error = e;
27 hasError = true;
28 await returnAsyncIterator(it);
29 break;
30 }
31 yield c;
32 }
33 if (!hasError) {
34 break;
35 }
36 }
37 if (hasError) {
38 throw error;
39 }
40 }
41}
42/**
43 * Creates a sequence by concatenating source sequences until a source sequence completes successfully.
44 * @param {AsyncIterable<AsyncIterable<T>>} source Source sequences.
45 * @return {AsyncIterable<T>} Sequence that continues to concatenate source sequences while errors occur.
46 */
47export function catchAll(source) {
48 return new CatchAllAsyncIterable(source);
49}
50export function catchError(...args) {
51 return new CatchAllAsyncIterable(args);
52}
53
54//# sourceMappingURL=catcherror.mjs.map