A TypeScript helper for managing asynchronous jobs with support for concurrency, retry, delay, timeout, and caching.
const resolver = new AwaitQueue(async (name, signal) => {
const req = await fetch(`https://pokeapi.co/api/v2/pokemon/${name}`, {
method: 'GET',
signal,
})
const details = await req.json()
return details
}, {
concurrency: 2
})
const pokemonDetailsList = await Promise.all(
['pikachu', 'charmander', 'bulbasaur', 'squirtle'].map(name => resolver.job(name))
)
A function that takes input of type I and returns a Promise of type O.
- concurrency?: number - The maximum number of concurrent jobs that can be processed at the same time.
- interval?: number - The minimum time interval between job executions.
- retry?: number - The number of times to retry a job if it fails.
- timeout?: number - The maximum time to wait for a job to complete before timing out.
- delay?: number - The delay before retry the job, in milliseconds.
- signal?: AbortSignal - An optional AbortSignal to cancel the job.
- cache?: ResolverCache<I, O> - An optional cache to store results of previous jobs.
job(input: I): Promise<O>
jobs(inputs: I[]): Promise<O>[]
Postprocessing function, receive a result of resolver and allow to call a recursive job with independent timeout or retry counter.