Files
test-repo/src/utils/try-combinations.js
T
fisker Cheungandautofix-ci[bot] ca5c11e95d Enable @prettier/plugin-oxc in playground (#18315)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-11-25 02:13:03 +08:00

42 lines
1.0 KiB
JavaScript

/**
* @template {function} T
* @param {T[]} combinations
* @returns {ReturnType<T>}
*/
function tryCombinationsSync(combinations) {
const errors = [];
for (const fn of combinations) {
try {
return fn();
} catch (error) {
errors.push(error);
}
}
// TODO: Use `AggregateError` when we drop Node.js v14
// throw new AggregateError(errors, "All combinations failed");
throw Object.assign(new Error("All combinations failed"), { errors });
}
/**
* @template {function} T
* @param {T[]} combinations
* @returns {Promise<ReturnType<T>>}
*/
async function tryCombinations(combinations) {
const errors = [];
for (const fn of combinations) {
try {
return await fn();
} catch (error) {
errors.push(error);
}
}
// TODO: Use `AggregateError` when we drop Node.js v14
// throw new AggregateError(errors, "All combinations failed");
throw Object.assign(new Error("All combinations failed"), { errors });
}
export { tryCombinations, tryCombinationsSync };