AsyncQueue

Namespace

AsyncQueue

Source:

Classes

exports.Queue

Methods

(static) Queue#add(fn, callback, callback)

Description:
  • Add an function to the queue Takes in a function that returns a Promise

Source:
Example
const queue = new Queue()

//function returns the promise we want to add to queue
const pets = () =>{
  return new Promise((resolve) =>{
    setTimeout(resolve, 100)
  })
}

//the callback that is ran on the settlement of the promise
const callback = (res) => {
  //do something with response
}

const error = (err) => {
  throw new Error(err)
}

//Adding the promise to the queue
queue.add(pets, callback, error)
Parameters:
Name Type Description
fn promiseFunction

The function that returns a promise you want to add to the queue

callback resCallback

The function that is executed when the promise resolves

callback errCallback

The function that is executed when the promise rejects

(static) Queue#setMaxConcurrency(maxConcurrency)

Description:
  • Set the max amount of promises to run concurrently after queue initialization

Source:
Parameters:
Name Type Description
maxConcurrency number

The max amount of promises to run concurrently

(static) Queue#setMaxRetries(maxRetries)

Description:
  • Set the max amount of times a promise can be retried after a failure By default the queue will not retry a failed promise.

Source:
Example
const queue = new Queue()

//setting retries to 3
queue.setRetries(3)

const pets = () =>{
  return new Promise((resolve, reject) =>{
    setTimeout(reject('rejected'), 100)
  })
}

const callback = (res) => {
  //do something with data
}

const errCallback = ( err) => {
  console.log(err.message) // output: 'max retries reached'
  console.log(err.errors) //  output: list of errors
}

queue.add(pets, callback, errCallback)
Parameters:
Name Type Description
maxRetries number

The max amount of promises to run concurrently

(static) Queue#setPromiseTimeout(timeout)

Description:
  • Set the max amount of time a promise can take to settle By default the queue will not monitor the promise time to settle a signal must be handled in the promise for the timeout to abort the promise

Source:
To Do:
  • implement abort controller to kill promise when timeout is reached
Example
const queue = new Queue()

//setting timeout for promises to 100ms
queue.setPromiseTimeout(100)

//function returns the promise we want to add to queue
const pets = (signal) =>{
  return new Promise((resolve, reject) =>{
  signal.addEventListener("abort", () => {
   reject("Aborted")
  }
    setTimeout(resolve, 500) //note that the timeout in the promise is larger than the set promise timeout
  })
}

//the callback that is ran on the resolution of the promise
const callback = (res ) => {
  //do something with data
}

//the callback that is ran on the rejection of the promise
const errCallback = (err) => {
console.log(err) //output: "Request timed out"
}

//Adding the promise to the queue
queue.add(pets, callback, errCallback)
Parameters:
Name Type Description
timeout number

The max amount of time in ms a promise can take to settle