You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
16 lines
562 B
16 lines
562 B
module.exports = async function race (p, min = 1, max = p.length) {
|
|
let errors = 0
|
|
const results = []
|
|
// avoid unhandled rejections after early return/throw
|
|
for (const promise of p) promise.catch(() => {})
|
|
for (let i = 0; i < p.length; i++) {
|
|
try {
|
|
const res = await p[i]
|
|
if (results.length < max) results.push(res)
|
|
if (results.length >= max) return results
|
|
if (results.length + errors === p.length) return results
|
|
} catch {
|
|
if ((p.length - ++errors) < min) throw new Error('Too many requests failed')
|
|
}
|
|
}
|
|
}
|
|
|