Skip to content

JavaScript utility to enqueue async tasks for Node.js and the browser

Notifications You must be signed in to change notification settings

irbux/awaitqueue

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AwaitQueue

JavaScript utility to enqueue async tasks for Node.js and the browser.

Installation

$ npm install awaitqueue

Usage

  • CommonJS usage:
const AwaitQueue = require('awaitqueue');
  • ES6 usage:
import AwaitQueue from 'awaitqueue';

API

new AwaitQueue({ ClosedErrorClass = Error })

Creates an AwaitQueue instance.

  • @param {Error} ClosedErrorClass: Custom Error derived class that will be used to reject pending tasks after close() method has been called. If not set, Error class is used.

async awaitQueue.push(task)

Accepts a task as argument and enqueues it after pending tasks. Once processed, the push() method resolves (or rejects) with the result returned by the given task.

  • @param {Function} task: Function that must return a Promise or a value directly.

awaitQueue.close()

Closes the queue. Pending tasks will be rejected with ClosedErrorClass error.

Usage example

const AwaitQueue = require('awaitqueue');

const queue = new AwaitQueue();
let taskCounter = 0;

async function task()
{
  return new Promise((resolve) =>
  {
    setTimeout(() =>
    {
      ++taskCounter

      console.log('task %d done!', taskCounter);

      resolve(taskCounter);
    }, 2000);
  }); 
}

async function run()
{
  let ret;

  console.log('calling queue.push()');
  ret = await queue.push(task);
  console.log('>>> ret:', ret);

  console.log('calling queue.push()');
  ret = await queue.push(task);
  console.log('>>> ret:', ret);
  
  console.log('calling queue.close()');
  queue.close();

  try
  {
    console.log('calling queue.push()');
    ret = await queue.push(task);
    console.log('>>> ret:', ret);
  }
  catch (error)
  {
    console.error('>>> task failed: %s', error.toString());
  }
}

run();

Output:

calling queue.push()
// after 2 seconds:
task 1 done!
>>> ret: 1
calling queue.push()
// after 2 seconds:
task 2 done!
>>> ret: 2
calling queue.close()
calling queue.push()
>>> task failed: Error: AwaitQueue closed

Author

License

ISC

About

JavaScript utility to enqueue async tasks for Node.js and the browser

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%