Understand error behavior in concurrent execution
mainWhen using the concurrent operator, FxTS behaves similarly to Promise.all. If an error occurs in one of the concurrent tasks, other tasks that have already been initiated will continue to be evaluated. The error is caught by the try-catch block once the current batch of concurrent requests is processed or the error is propagated.
import { concurrent, filter, map, pipe, toArray, toAsync } from "@fxts/core";
const fetchAsyncError = (a) => {
if (a === 3) {
return Promise.reject(`err ${a}`);
}
return a;
};
try {
await pipe(
[
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3), // When this item is evaluated, `map` function throws an error.
Promise.resolve(4), // This item is also evaluated.
Promise.resolve(5), // Is is not evaluated from this item.
Promise.resolve(6),
],
toAsync,
map(fetchAsyncError),
filter((a) => a % 2 === 0),
concurrent(2), // request 2
toArray,
);
} catch (err) {
// handle err
}