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

promise-the-world

v1.0.1

Published

A complete Promise utils collection

Downloads

16,985

Readme

promise-the-world

A collection of utils to handle recurring patterns around Promises and async/await code.

Usage

The individual functions can be imported using the path promise-the-world/ + component name. For the defer function it would look like this:

const defer = require('promise-the-world/defer')

It's also possible to load the functions using the destructuring syntax. Using the path to the individual components is recommended to reduce the bundle sizes. Importing the components with the destructuring syntax would look like this:

const { defer } = require('promise-the-world')

defer()

Implements the obsolete Promise.defer() method which returns a Deferred object. In most cases, the Promise constructor is sufficient to create a Promise. defer can be used for the rare cases where resolve() or reject() must be called outside the executor callback of the Promise constructor. The returned Deferred object has the following properties/methods:

  • resolve(value): Resolves the Promise with the given value.
  • reject(error): Rejects the Promise with the given error.
  • promise: The Promise object

The following example uses defer to wrap the close event of a stream into a Promise:

const fs = require('fs')
const defer = require('promise-the-world/defer')

const closed = defer()
const stream = fs.createReadStream('README.md')

stream.once('close', closed.resolve)
stream.resume()

closed.promise.then(() => {
  console.log('closed')
})

delay(time)

Returns a Promise that resolves after the given time in milliseconds.

The following example code simply waits for 1s.

const delay = require('promise-the-world/delay')

delay(1000).then(() => {
  console.log('1s later')
})

mutex()

Returns a mutex/lock object. A mutex is useful to limit or sync access to a resource. Access can be requested with the lock method which will resolve immediately if the mutex is not locked. In the case that the mutex is locked, the lock method will resolve after unlock was called. The returned mutex object has the following methods:

  • lock(): Request access to the mutex. Returns a Promise which will resolve when the mutex is not / no longer locked.
  • unlock: Unlocks a locked mutex.

The following example wraps fetch and uses a mutex to do only one request at a time:

const mutex = require('promise-the-world/mutex')

const access = mutex()

async function limitedFetch (url) {
  await access.lock()

  try {
    const result = await fetch(url)

    access.unlock()

    return result
  } catch (err) {
    // make sure you unlock the mutex if an error was thrown!
    access.unlock()

    throw err
  }
}

Promise.all([
  limitedFetch('http://example.org/'),
  limitedFetch('http://example.com/')
])

queue(maxPending, maxQueued)

Returns a queue object to handle Promises in sequence. With the maxPending option, it's possible to limit the items handled in parallel. That can be useful for cases where resources are limited or expensive. The Promises must be wrapped in factory function to give the queue control over the time when the items should be started.

  • maxPending: The limit for the pending Promises. The value is copied from the constructor argument. The default value is 1.
  • maxQueued: The limit of queued factories. If the limit is reached, .add() will throw an error. The default value is Infinity
  • .add(factory): Adds a Promise wrapped in a factory to the queue. Returns a Promise which acts as a proxy for the actual Promise.
  • .length: The number of queue factories.
  • .pending: The number of pending Promises.

The following example uses a queue to limit the number of parallel fetch request to a maximum of 2:

const queue = require('promise-the-world/queue')

const fetchQueue = queue(2)

Promise.all([
  fetchQueue.add(() => fetch('http://example.org/1')),
  fetchQueue.add(() => fetch('http://example.org/2')),
  fetchQueue.add(() => fetch('http://example.org/3')),
  fetchQueue.add(() => fetch('http://example.org/4'))
])