// Run `fn` over items with at most `limit` in flight; results keep input order.
export async function mapLimit(items, limit, fn) {
const out = new Array(items.length)
let next = 0
const worker = async () => {
while (next < items.length) {
const i = next++
out[i] = await fn(items[i], i)
}
}
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker))
return out
}