Files
renovate/lib/util/promises.ts
Sergei Zharinov 930cf6633c refactor: Rewrite imports with .ts extensions (#40700)
fix: Additional vi.mock path corrections and prettier formatting
2026-01-26 22:48:17 +00:00

71 lines
1.6 KiB
TypeScript

import type { Options as PAllOptions } from 'p-all';
import pAll from 'p-all';
import type { Mapper, Options as PMapOptions } from 'p-map';
import pMap from 'p-map';
import { logger } from '../logger/index.ts';
import { ExternalHostError } from '../types/errors/external-host-error.ts';
type PromiseFactory<T> = () => Promise<T>;
function isExternalHostError(err: any): err is ExternalHostError {
return err instanceof ExternalHostError;
}
function handleMultipleErrors(errors: Error[]): never {
const hostError = errors.find(isExternalHostError);
if (hostError) {
throw hostError;
}
if (
errors.length === 1 ||
new Set(errors.map(({ message }) => message)).size === 1
) {
const [error] = errors;
throw error;
}
throw new AggregateError(errors);
}
function handleError(err: any): never {
if (!(err instanceof AggregateError)) {
throw err;
}
logger.debug({ err }, 'Aggregate error is thrown');
handleMultipleErrors(err.errors);
}
export async function all<T>(
tasks: PromiseFactory<T>[],
options?: PAllOptions,
): Promise<T[]> {
try {
const res = await pAll(tasks, {
concurrency: 5,
stopOnError: false,
...options,
});
return res;
} catch (err) {
return handleError(err);
}
}
export async function map<Element, NewElement>(
input: Iterable<Element>,
mapper: Mapper<Element, NewElement>,
options?: PMapOptions,
): Promise<NewElement[]> {
try {
const res = await pMap(input, mapper, {
concurrency: 5,
stopOnError: false,
...options,
});
return res;
} catch (err) {
return handleError(err);
}
}