@botonic/plugin-contentful
Version:
## What Does This Plugin Do?
45 lines • 1.49 kB
JavaScript
import { __awaiter } from "tslib";
import * as Parallel from 'async-parallel';
import { MultiError } from 'async-parallel';
/**
* async-parallel makes code simpler and allows limiting concurrency
*/
export function asyncMap(context, list, action, concurrency, errorTreatment) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield Parallel.map(list, resumable(action, errorTreatment), concurrency || context.concurrency);
return result.filter((i) => i !== undefined);
});
}
export function resumable(action, errorTreatment) {
const wrapped = (value, index, list) => __awaiter(this, void 0, void 0, function* () {
try {
return yield action(value, index, list);
}
catch (e) {
if (errorTreatment) {
return errorTreatment(value, e);
}
throw e;
}
});
return wrapped;
}
export function asyncEach(context, list, action, concurrency) {
return __awaiter(this, void 0, void 0, function* () {
concurrency = concurrency || context.concurrency;
yield Parallel.each(list, action, concurrency);
});
}
export function reduceMultiError(error) {
let reduced = [];
for (const e of error.list) {
if (e instanceof MultiError) {
reduced = reduced.concat(reduceMultiError(e));
}
else {
reduced.push(e);
}
}
return reduced;
}
//# sourceMappingURL=async.js.map