TS Promise Array Progress CLI
async progressPromises<T>(promises: Array<Promise<T>>, title: string): Promise<Array<T>> {
const bar = new progress.Bar({
format: title + ' |' + colors.cyan('{bar}') + '| {percentage}% || {value}/{total} Chunks || Speed: {speed}',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true,
})
bar.start(promises.length, 0, { speed: 'N/A' })
let started = new Date().getTime()
let finished = 0
for (const promise of promises)
promise.then(() => {
const interval: number = (new Date().getTime() - started) / (1000 * (finished + 1))
bar.update(++finished, { speed: `${String(interval).slice(0, 5)} 초/검색` })
})
const results = await Promise.all(promises)
bar.stop()
console.log()
return results
}
TS Array Chunk
async asyncChunkRequest<Input, Output>(
inputs: Array<Input>,
method: (inputs: Array<Input>, ...args: unknown[]) => Array<Output>,
args: unknown[] = [],
chunkSize: number = 100
): Promise<Array<Output>> {
const results: Array<Output> = []
for (const index in Array.from({ length: Math.ceil(inputs.length / chunkSize) })) {
const chunk = inputs.slice(Number(index) * chunkSize, (Number(index) + 1) * chunkSize)
const chunkResult = await method(chunk, ...args)
results.push(...chunkResult)
}
return results
}