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 🙏

© 2021 – Pkg Stats / Ryan Hefner

pique

v1.0.0

Published

Promise Queue Library

Downloads

7

Readme

Build Status GitHub version NPM version Downloads

Pique - A Promise Queue for JavaScript

Pique is a library that creates a queue of functions that are guaranteed to be run exactly once and in serial FIFO order. So that the first enqueue function will complete before the second is executed.

Below is the basic usage of the priority queue by adding items to the queue with the push method.

const Pique = require('pique');

const pq = new Pique();

const firstPromise = pq.push((resolve, reject) => {
  $http.get(gameData).then(resolve).catch(reject);
}).then(result => {
  populateGameData(result);
}).catch(err => {
  handleError(err);
});

const secondPromise = pq.push((resolve, reject) => {
  $http.get(playerData).then(resolve).catch(reject);
}).then(result => {
  populdatePlayerData(result);
}).catch(err => {
  handleError(err);
});

We can also add items to the front of the queue with the unshift method.

const Pique = require('pique');

const pq = new Pique();

const firstPromise = pq.push((resolve, reject) => {
  $http.get(gameData).then(resolve).catch(reject);
}).then(result => {
  populateGameData(result);
}).catch(err => {
  handleError(err);
});

// Add to the front of the list
const secondPromise = pq.unshift((resolve, reject) => {
  $http.get(playerData).then(resolve).catch(reject);
}).then(result => {
  populdatePlayerData(result);
}).catch(err => {
  handleError(err);
});

To show the push versus unshift in action, let's take a contrived example of putting several promises together and we will show the results in different orders based upon which method we used.

const Pique = require('pique');

const pq = new Pique();

// Set the first for 20 seconds
pq.push((res) => setTimeout(() => res(42), 20000))
  .then((x) => console.log(x));

// Set the second up to first at 15 seconds
pq.unshift((res) => setTimeout(() => res(56), 15000))
  .then((x) => console.log(x));

// Set the second up one at 10 seconds
pq.unshift((res) => setTimeout(() => res(78), 10000))
  .then((x) => console.log(x));

// Finally add one for 5 seconds
pq.push((res) => setTimeout(() => res(96), 5000))
  .then((x) => console.log(x));

// => 78
// => 56
// => 42
// => 96

If you are not in an environment with a native Promise, you can set your own using the configure method to specify which ES2015 compliant Promise implementation you like.

const Pique = require('pique');

Pique.configure(require('lie'));

const pq = new Pique();

Installation

You can use Pique in both the browser and Node.js.

NPM

You can install Pique by installing via NPM.

$ npm install pique

Browser

Of you can reference the pique.js or pique.min.js in your browser.

<script src="pique.min.js"></script>

Documentation

Pique constructor

Pique Class Methods

Pique Instance Methods

Pique Constructor

Pique()

Creates a queue of functions that are guaranteed to be run exactly once and in serial FIFO order. So that the first enqueue function will complete before the second is executed.

Example

const Pique = require('pique');

const pq = new Pique();

pq.push((resolve, reject) => {
  setTimeout(() => res(42), 5000);
}).then(result => {
  console.log(result);
});

Pique Class Methods

Pique.configure(promiseCtor)

Configures which Promise constructor to use for creating Promises for chaining. Note that this can be any Promise implementation that is ES2015 compliant.

Arguments

  1. promiseCtor: Object - A Promise constructor used to create promises. Note that this must be ES2015 compliant to support both then and catch methods.

Example

const Pique = require('pique');

Pique.configure(require('lie'));

const pq = new Pique();

Pique.prototype.push(fn)

Enqueue a function to be executed only when all the functions enqueued before it have been executed.

Arguments

  1. fn: Function - This is the callback function used in a Promise. The function that will be called when its turn in the queue comes up. The function takes as arguments the resolve and reject functions used with promises. So the function can run asynchronously and indicate their success or failure using the resolve or reject function arguments. This takes the following arguments:
    • resolve: Function - used to resolve the Promise.
    • reject: Function - used to reject the Promise.

Returns

Promise - A promise that will resolve or be rejected depending on the outcome of the submitted function.

Example

const Pique = require('pique');

const pq = new Pique();

pq.push((resolve, reject) => {
  setTimeout(() => res(42), 5000);
}).then(result => {
  console.log(result);
});

pq.push((resolve, reject) => {
  setTimeout(() => res(56), 5000);
}).then(result => {
  console.log(result);
});

// => 42
// => 56

Pique.prototype.unshift(fn)

Enqueue a function at the head of the queue. It will be executed as soon as the current promise (if any) resolves, before anyone who called enqueue. If multiple calls are made to enqueueAtTop then they will be enqueued at the head of the queue in LIFO order.

Arguments

  1. fn: Function - This is the callback function used in a Promise. The function that will be called when its turn in the queue comes up. The function takes as arguments the resolve and reject functions used with promises. So the function can run asynchronously and indicate their success or failure using the resolve or reject function arguments. This takes the following arguments:
    • resolve: Function - used to resolve the Promise.
    • reject: Function - used to reject the Promise.

Returns

Promise - A promise that will resolve or be rejected depending on the outcome of the submitted function.

Example

const Pique = require('pique');

const pq = new Pique();

pq.push((resolve, reject) => {
  setTimeout(() => res(42), 5000);
}).then(result => {
  console.log(result);
});

pq.unshift((resolve, reject) => {
  setTimeout(() => res(56), 1000);
}).then(result => {
  console.log(result);
});

// => 56
// => 42

LICENSE

The MIT License (MIT)

Copyright (c) 2016 Microsoft Corporation

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.