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

phold

v1.0.0

Published

Hold promise rejection until requested

Downloads

616

Readme

phold

Hold promise rejection until requested

Usage

phold(promise)

Assigns an empty error handler on the promise to avoid unhandledRejection and returns a function that returns a promise which resolves with the original value or rejects with the original error.

Example

const { phold } = require('phold');

async function example() {
    
    // capture any rejections so you don't get the unhandledRejection warning/crash
    const giveItBack = phold(somethingThatCouldReject());
    
    // do more work
    await someOtherAsyncStuff();
    
    try {
        // resolve or reject with original result, if any
        await giveItBack();
    }
    catch (err) {
        // will be caught if somethingThatCouldReject() rejects
    }
}

Use case

Suppose you have an asynchronous subscription channel, where you are waiting for a single message or a single error notification to arrive (e.g. notifications about jobs in a queue). The notification is triggered by a request. This means that you need to subscribe for the message before you make the request (otherwise there is a chance you'll miss the notification if the job completes really really fast).

The subscription channel is probably an event emitter, but since we're only waiting for a single message, i.e. we have a concrete final state ("job completed" or "job failed") - a promise is a nice abstraction to use to hold the value until we need it. Your code might look something like this:

async function queueExample() {

    const myJob = await createJob();
    
    const subscription = new Promise((resolve, reject) => {
        
        jobQueue
          .on('completed', (job) => {
    
              if (job.id === myJob.id) {
                  resolve(job);
              }
          })
          .on('error', (err) => {
              
              if (err.job.id === myJob.id) {
                  reject(err);
              }
          });
    });
    
    const started = await startJob(myJob);
    
    try {
        return await subscription;
    }
    catch (err) {
        return retry(myJob)
    }
}

Note how startJob is an async function - this means we're breaking the event loop without assigning a catch() handler for the subscription promise. This means that if error event arrives before startJob completes (or if you have some other async steps before you actually need the subscription) - you will get this very nice warning in your console (node 6.6.0+):

(node:9260) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:9260) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

The scary part is the DeprecationWarning - in the future such code would crash your process - and this is absolutely not what you want, because you're perfectly able to handle the rejection (e.g. by retrying the operation).

phold fixes this by assigning a catch() handler to your promise to hold the rejection until you're ready to use it.