npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

queue-as-promised

v1.0.1

Published

A JavaScript library for queueing up tasks, both synchronous and asynchronous.

Downloads

14

Readme

Build Status JavaScript Standard Style Guide

queue-as-promised

A JavaScript library for queueing up tasks, both synchronous and asynchronous.

Install

npm install queue-as-promised

Overview

You have some tasks to perform and you need to queue them up. Maybe because they all share a resource so you can only have one running at a time. Doesn't matter why. You define a function that will run your task, and register with a Queue. The registered tasks will executed one at a time, in order, and notify you when they're done.

Usage

import {Queue} from 'queue-as-promised'

const q = new Queue()

q.enqueue(() => {
  doSomeSynchronousWork()
})
  .then(() => {
    console.log('Synchronous task complete')
  })

q.enqueue(() => {
  return promiseToDoSomeWork()
}
  .then(() => {
    console.log('Asynchronous task complete')
  })

In Depth

For our purposes, a task is some unit of work that has a definitive end point, though the end point is not necessarily known in advance. This work could be completely synchronous, or it could be asynchronous.

You create a new instance of a Queue and register your task with it by calling the enqueue method. You pass in a function which will actually perform your task. This function will be invoked by the queue once all previously registered tasks are completed.

If your task function is completely synchronous, then returning normally from the function will indicate that your task is complete. Likewise, if an exception is thrown from your task function, the task is considered to be complete.

If your task requires asynchronous work, then your task function should return a promise which will settle when your task is complete. Whether your promise fulfills or rejects is immaterial, the act of settling to either of those states causes the queue to consider the task complete.

The enqueue method also returns a promise of its own, which will settle based on your task function. Specifically, it will fulfill once your task completes successfully (returns normally or returns a promise which eventually fulfills), or will reject if your task fails (either the task function throws an error, or it returns a promise that eventually rejects).

If your task function results in some value, the returned promise will fulfill with it. If your task function fails for some error, the returned promise will reject with that reason.

The returned promise allows you to take action in response to the completion of your task, without prolonging the task from the point of view of the queue. While you can always cram additional work into your task function, the queue will not move on to the next task until that work is completed. If there is follow on work that you want to do, but don't want to block subsequent tasks because of it, then you should chain that work onto the returned promise instead of inside the task function.

You can optionally pass in an initial parameter which is called the item. This value will be passed along as the only argument to the provided task function. This allows you to reuse the same task function for different tasks, which can then be parameterized by the item.

Note that the item will be serialized and deserialized before being passed to the task function. If an error occurs during this process, then the promise returned by the call to enqueue will reject. For instance, objects with circular references will err during serialization.

API

Queue::enqueue([item, ] taskFunction)

Queue::enqueue<I, R>([item: I, ] taskFunction: (I) -> R | (I) -> Promise<R>): Promise<R>`

Registers the given taskFunction as a task in the queue.

parameters

  • item - An optional value which will be serialized and deserialized, then passed to your task function when it is called.

  • taskFunction - The actual task function which will be called when it reaches the front of the queue, with the provided item passed in as the sole argument (after being serialized and deserialized).

    It can return a value of any type, or return a promise for any type. The promise returned by enqueue will fulfill with the returned value, or adopt the state of the returned promise.

returns

A promise for the value returned by taskFunction, or a promise which adopts the state of a promise returned by the task function. If the task function throws an error, the returned promise will reject with that error. Additionally, if an error occurs attempting to serialize the given item, the returned promise will reject. It is not specified when this rejection will occur, it does not necessarily wait for the task to reach the front of the queue.

Promises

For all parameters specified as being "Promises", any thennable can be used and will be resolved to a promise according to the Promises/A+ specification. Returned promises also follow the A+ spec, but the specific implementation is not guaranteed. If you need a promise of particular implementation, you should use that implementation's utilities to convert it to a promise of that type (e.g., Bluebird.resolve)