fastq

repository·master·Indexed 22 days ago

https://github.com/mcollina/fastq

A high-performance, in-memory work queue for Node.js (version 1.20.1) that manages tasks with controlled concurrency. It supports both a traditional callback API and a modern Promise-based API via fastq.promise(), with full TypeScript type definitions available for both workflows.

Tokens
2.8K
Snippets
10
Records
19
Agent score
28%

What's inside fastq

  1. Set the execution context (this) for workers

    master

    When creating a queue, you can optionally provide a that argument as the first parameter to fastq() or fastq.promise(). This object will be used as the this context inside your worker function.

    'use strict'
    
    const that = { hello: 'world' }
    const queue = require('fastq')(that, worker, 1)
    
    queue.push(42, function (err, result) {
      if (err) { throw err }
      console.log(this) // Accesses { hello: 'world' }
      console.log('the result is', result)
    })
    
    function worker (arg, cb) {
      console.log(this) // Accesses { hello: 'world' }
      cb(null, arg * 2)
    }
  2. Use fastq with TypeScript

    master

    Fastq provides type definitions for both callback and promise APIs.

    Callback API: Use the queue and done types.

    Promise API: Use the queueAsPromised type.

    // Callback API
    import * as fastq from "fastq";
    import type { queue, done } from "fastq";
    
    type Task = { id: number }
    const q: queue<Task> = fastq(worker, 1)
    
    q.push({ id: 42 })
    
    function worker (arg: Task, cb: done) {
      console.log(arg.id)
      cb(null)
    }
    
    // Promise API
    import type { queueAsPromised } from "fastq";
    
    const qP: queueAsPromised<Task> = fastq.promise(asyncWorker, 1)
    
    qP.push({ id: 42 }).catch((err) => console.error(err))
    
    async function asyncWorker (arg: Task): Promise<void> {
      console.log(arg.id)
    }
  3. Use the fastq callback API

    master

    The standard callback API allows you to create a queue by passing a worker function and a concurrency limit. Tasks are added using queue.push(task, done), where done is a callback invoked with (err, result) once the task is processed.

    'use strict'
    
    const queue = require('fastq')(worker, 1)
    
    queue.push(42, function (err, result) {
      if (err) { throw err }
      console.log('the result is', result)
    })
    
    function worker (arg, cb) {
      cb(null, arg * 2)
    }
  4. Use the fastq.promise() API

    master

    Creates a queue optimized for Promise-based workflows. It includes all standard queue methods plus promise-specific versions of push, unshift, and drained.

    Arguments:

    • that (optional): The context (this) for the worker function.
    • worker (function): A function that must return a Promise.
    • concurrency (number): The number of concurrent tasks.

    Promise Methods:

    • queue.push(task): Returns a Promise that fulfills/rejects when the task completes. This promise can be ignored without causing unhandled rejections.
    • queue.unshift(task): Returns a Promise that fulfills/rejects when the task completes.
    • queue.drained(): Returns a Promise that resolves when all tasks in the queue have been processed.
  5. Control queue execution with pause() and resume()

    master

    Control the flow of task processing:

    • queue.pause(): Pauses the processing of new tasks. Tasks currently being worked on are not stopped.
    • queue.resume(): Resumes task processing.
    • queue.paused (Read-Only): A boolean property indicating if the queue is currently paused.
  6. Monitor queue state and length

    master

    Check the status of the queue:

    • queue.length(): Returns the number of tasks currently waiting in the queue.
    • queue.idle(): Returns true if no tasks are being processed or waiting; false otherwise.
    • queue.getQueue(): Returns an array of all tasks currently waiting in the queue.
  7. Terminate the queue with kill() and killAndDrain()

    master

    Stop the queue and clear pending tasks:

    • queue.kill(): Removes all waiting tasks and resets the drain function to an empty function.
    • queue.killAndDrain(): Same as kill, but the drain function is called before the reset.
  8. Manage queue tasks with push() and unshift()

    master

    Add tasks to the queue:

    • queue.push(task, done): Adds a task to the end of the queue. In the promise API, push(task) returns a Promise.
    • queue.unshift(task, done): Adds a task to the beginning of the queue. In the promise API, unshift(task) returns a Promise.
  9. Create a new queue with fastqueue()

    master

    Creates a new queue instance.

    Arguments:

    • that (optional): The context (this) for the worker function.
    • worker (function): The worker function that processes tasks.
    • concurrency (number): The number of tasks to execute in parallel.
  10. Configure queue lifecycle hooks and error handling

    master

    Customize how the queue behaves during specific lifecycle events or errors:

    • queue.error(handler): Sets a global error handler. The handler is called as handler(err, task) whenever a task fails.
    • queue.drain: A function called when the last item in the queue has been processed. Can be altered at runtime.
    • queue.empty: A function called when the last item in the queue has been assigned to a worker. Can be altered at runtime.
    • queue.saturated: A function called when the queue reaches its concurrency limit. Can be altered at runtime.
    • queue.concurrency: Returns the current concurrency limit. This value can be altered at runtime.
  11. Handle errors globally with error()

    master

    Set a global error handler for the queue using error(handler). This handler will be called whenever a task fails (i.e., when the worker calls worked(err, result) with a truthy err).

    handler(err, value): err is the error object, and value is the original task value.

    queue.error((err, val) => {
      console.error(`Task ${val} failed with: ${err.message}`);
    });