Basic usage of @henrygd/queue
mainTo use the queue, create a new instance using newQueue(concurrency) and add async functions or promise-returning functions using the add method. The add method returns a promise that resolves with the same value as the supplied function. You can use queue.done() to wait for all tasks in the queue to complete.
import { newQueue } from '@henrygd/queue'
// create a new queue with a concurrency of 2
const queue = newQueue(2)
const pokemon = ['ditto', 'hitmonlee', 'pidgeot', 'poliwhirl', 'golem', 'charizard']
for (const name of pokemon) {
queue.add(async () => {
const res = await fetch(`https://pokeapi.co/api/v2/pokemon/${name}`)
const json = await res.json()
console.log(`${json.name}: ${json.height * 10}cm | ${json.weight / 10}kg`)
})
}
console.log('running')
await queue.done()
console.log('done')