Files
dzc f55561c98d feat:上传到阿里云oss
feat:把以前的图片迁移到阿里云oss
2026-11-05 15:22:55 +08:00

31 lines
895 B
JavaScript

/**
* Map in input order with bounded parallelism. After the first failure, stop
* scheduling work and drain every in-flight worker before rejecting.
*/
export async function mapBounded(items, limit, worker) {
if (!Number.isSafeInteger(limit) || limit < 1) throw new Error('并发数量必须为正整数');
const source = Array.from(items);
const results = new Array(source.length);
let next = 0;
let failed = false;
let firstError;
async function run() {
while (!failed && next < source.length) {
const index = next++;
try {
results[index] = await worker(source[index], index);
} catch (error) {
if (!failed) {
failed = true;
firstError = error;
}
}
}
}
await Promise.all(Array.from({ length: Math.min(limit, source.length) }, () => run()));
if (failed) throw firstError;
return results;
}