UNPKG

2.51 kBJavaScriptView Raw
1import { AsyncIterableX } from './asynciterablex';
2import { returnAsyncIterator } from '../util/returniterator';
3import { wrapWithAbort } from './operators/withabort';
4import { throwIfAborted } from '../aborterror';
5export class CatchAllAsyncIterable extends AsyncIterableX {
6 constructor(source) {
7 super();
8 this._source = source;
9 }
10 async *[Symbol.asyncIterator](signal) {
11 throwIfAborted(signal);
12 let error = null;
13 let hasError = false;
14 for (const source of this._source) {
15 const it = wrapWithAbort(source, signal)[Symbol.asyncIterator]();
16 error = null;
17 hasError = false;
18 while (1) {
19 let c = {};
20 try {
21 const { done, value } = await it.next();
22 if (done) {
23 await returnAsyncIterator(it);
24 break;
25 }
26 c = value;
27 }
28 catch (e) {
29 error = e;
30 hasError = true;
31 await returnAsyncIterator(it);
32 break;
33 }
34 yield c;
35 }
36 if (!hasError) {
37 break;
38 }
39 }
40 if (hasError) {
41 throw error;
42 }
43 }
44}
45/**
46 * Continues an async-iterable sequence that is terminated by an exception with the next async-iterable sequence.
47 *
48 * @export
49 * @template T The type of the elements in the source and handler sequences.
50 * @param {Iterable<AsyncIterable<T>>} source async-iterable sequences to catch exceptions for.
51 * @returns {AsyncIterableX<T>} An async-iterable sequence containing elements from consecutive source
52 * sequences until a source sequence terminates successfully.
53 */
54export function catchAll(source) {
55 return new CatchAllAsyncIterable(source);
56}
57/**
58 * Continues an async-iterable sequence that is terminated by an exception with the next async-iterable sequence.
59 *
60 * @export
61 * @template T The type of the elements in the source and handler sequences.
62 * @param {...AsyncIterable<T>[]} args async-iterable sequences to catch exceptions for.
63 * @returns {AsyncIterableX<T>} An async-iterable sequence containing elements from consecutive source
64 * sequences until a source sequence terminates successfully.
65 */
66export function catchError(...args) {
67 return new CatchAllAsyncIterable(args);
68}
69
70//# sourceMappingURL=catcherror.mjs.map