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 🙏

© 2025 – Pkg Stats / Ryan Hefner

promise-mate

v1.1.0

Published

Delegate async responses

Readme

promise-mate

Promise.all fulfillment for many Promises to many callbacks to prevent duplicate calls when there are shared requirements.

Use cases

Use promise-mate if you have multiple Promises to run synchronously that are required by multiple callbacks, some of which have overlapping requirements. This is particularly useful for modular callbacks that may not always be called as well as for resource fetching.

Installation

npm i promise-mate

Or,

yarn add promise-mate

Example

The below example fetches data to update the webpage.

import Mate from 'promise-mate'

const runner = new Mate({
  users() {
    return fetch('/api/users').then(res => res.json()).then(json => json.users)
  },
  purchases() {
    return fetch('/api/purchases').then(res => res.json()).then(json => json.purchases)
  },
  products() {
    return fetch('/api/products').then(res => res.json()).then(json => json.products)
  },
  calendar() {  // Note: this Promise definition is not requested below so will not be run
    return fetch('/api/calendar').then(res => res.json()).then(json => json.calendar)
  }
});

runner
  .all([
    {
      requires: 'users',
      then(users) {
        // Update the user list!
      }
    },
    {
      requires: ['products', 'purchases'],
      then(products, purchases) {
        // Update product list to show if something's been bought
      }
    },
    {
      requires: [{ uuid: 1265723 }, 'users', 'products', 'purchases'],
      then(userData, users, products, purchases) {
        // Show user's latest purchases
      }
    }
  ])
  .then(() => { console.log('Page updated!') })
  .catch(() => { console.error('Error updating page') })

API

Mate.all

Run all actions.

Mate.all(definitions, actions)

Arguments

  • definitions - Key-indexed object with values that are functions that return Promises (or something else).
  • actions - Array of objects defining requirements and callbacks.
    • actions[i].requires - Requirements to be passed as arguments to the callback. Can be a singular requirement or an array of requirements. Singular requirements cannot be an array. If a requirement is not a key in definitions, will resolve the requirement.
    • actions[i].then - Function to call when requirements resolve. Passes results as arguments in order of actions[i].requires.

Returns

  • Promise<any[]> - An array of what each action's callback returns.

new Mate

Create a Mate with stored definitions.

var runner = new Mate(definitions)

Arguments

  • definitions (optional) - Key-indexed object with values that are functions that return Promises (or something else).

Mate.prototype.define

Store a Promise generating function.

runner.define(key, definition)

Arguments

  • key - Key for definition.
  • definition - Function that returns a Promise (or something else since Promise.all will just resolve non-promises).

Returns

  • The Mate instance.

Mate.prototype.undefine

Delete a definition

runner.undefine(key)

Arguments

  • key - Key for deletion.

Returns

  • The Mate instance.

Mate.prototype.all

Run all actions.

runner.all(actions)

Arguments

  • actions - Array of objects defining requirements and callbacks.
    • actions[i].requires - Requirements to be passed as arguments to the callback. Can be a singular requirement or an array of requirements. Singular requirements cannot be an array. If a requirement is not a key in definitions, will resolve the requirement.
    • actions[i].then - Function to call when requirements resolve. Passes results as arguments in order of actions[i].requires.

Returns

  • Promise<any[]> - An array of what each action's callback returns.