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

deferp

v0.0.1

Published

Defer Promise's result

Downloads

6

Readme

deferp

Defer the results of Promise.

Installation

yarn add deferp

Usage

const deferp = require("deferp");
(async () => {
  const deferred = deferp(asyncFunc());
  await anotherAsyncFunc();
  await deferred(); // -> receive the result regardless of whether asyncFunc is complete or not
})();

Why should be wrapped with deferp?

Events that will be emitted at the time of the subsequent Promise will need to listen before the emit. If await for the event listener to be called at that time, it will be blocked, so you must await after the emit. When writing such code, there are cases where calling of the event handler has ended at the timing of await. If rejected at such timing, try-catch can not be done even by the caller.

const EventEmitter = require("events");
const emitter = new EventEmitter();
const wait = delay => {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, delay);
  });
};
const waitForEvent = async event => {
  return new Promise((resolve, reject) => {
    emitter.on(event, () => {
      reject(new Error("catch me if you can"));
    });
  });
};

(async () => {
  try {
    const promise = waitForEvent("foo");
    await emitter.emit("foo");
    await wait(1000);
    await promise;
  } catch (err) {
    console.log("caught:", err);
  }
})();

This code seems to be able to catch an error. But, UnhandledPromiseRejectionWarning is warned by Node runtime and finally catching errors.

To avoid this, wrap with deferp:

(async () => {
  try {
    const deferred = deferp(waitForEvent("foo"));
    await emitter.emit("foo");
    await wait(1000);
    await deferred();
  } catch (err) {
    console.log("caught:", err);
  }
})();

The error can be caught at catch block properly.