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.
14 lines
454 B
14 lines
454 B
module.exports = async function race (p, min = 1, max = p.length) {
|
|
let errors = 0
|
|
const results = []
|
|
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')
|
|
}
|
|
}
|
|
}
|
|
|