Added fetching scripts
This commit is contained in:
18
scripts/utils/promiseAllStepN.ts
Normal file
18
scripts/utils/promiseAllStepN.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
export const promiseAllStepN = async <T>(n: number, list: (() => Promise<T>)[]) => {
|
||||
const head = list.slice(0, n)
|
||||
const tail = list.slice(n)
|
||||
const result: T[] = []
|
||||
const execute = async (promise: () => Promise<T>, i: number, runNext: () => Promise<void>) => {
|
||||
result[i] = await promise()
|
||||
await runNext()
|
||||
}
|
||||
const runNext = async () => {
|
||||
const i = list.length - tail.length
|
||||
const promise = tail.shift()
|
||||
if (promise !== undefined) {
|
||||
await execute(promise, i, runNext)
|
||||
}
|
||||
}
|
||||
await Promise.all(head.map((promise, i) => execute(promise, i, runNext)))
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user