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

passive-promise

v0.4.0

Published

Promise that's resolvable from outside of executor context.

Downloads

223

Readme

Passive Promise

Promise that's resolvable from outside of executor context.

It comes with super light-weighted bundle with 0 dependencies.

What is this?

This library exports the class named PassivePromise which works exactly like normal Promises, but can be resolved outside of promise executor context.

Installation

npm install passive-promise

or

yarn add passive-promise

The library supports pure ESM only and not exported as commonJS module syntax.

Usage Examples

Just use exactly like normal promise, but returned promise (which is extended Promise class) also comes with the resolve() and reject() method in it, which you can use to force resolve or reject the promise from outside.

All the chain methods such as then, catch, finally works as expected, but remind that the both resolve, reject method of any chained PassivePromise instance will propagate to the root PassivePromise instance regardless of its depth.

Common Usage

import PassivePromise from 'passive-promise';

(async () => {
  const foo = new PassivePromise((resolve, reject) => setTimeout(() => resolve(1), 1000));

  console.log(await foo); // 1 after 1 sec

  const bar = new PassivePromise((resolve, reject) => setTimeout(() => resolve(1), 1000));

  bar.resolve(-1);

  console.log(await bar); // -1 immedietly

  const baz = new PassivePromise((resolve, reject) => setTimeout(() => resolve(1), 1000));

  baz.reject(0) // Unhandled promise rejection.
})()

(async () => {
  const foo = new PassivePromise(() => {})
    .then((fulfilled) => console.log(fulfilled));
  
  foo.resolve(1); // 1

  
  const bar = new PassivePromise(() => {})
    .then((fulfilled) => console.log(fulfilled))
    .catch((errored) => console.log('Promise rejected!', errored));
  
  bar.reject(-1); // Promise rejected! -1

  const baz = new PassivePromise(() => {})
    .then(
      (fulfilled) => console.log(fulfilled),
      (errored) => console.log('Promise rejected!', errored),
    )
    .finally(() => console.log('Finally.'));

  baz.reject(-1) // Promise rejected! -1 Finally.
})()

Chain propagation

import PassivePromise from 'passive-promise';

(async () => {
  const chainedPromise = new PassivePromise(() => {})
    .then((firstRes) => {
      console.log('first.');
    })
    .then((secondRes) => {
      console.log('second.');
    })
    .then((thirdRes) => {
      console.log('third.');
    });
  
  chainedPromise.resolve(1); 

  await chainedPromise; // first. second. third.

  const middleChain = new PassivePromise(() => {})
    .then((firstRes) => {
      console.log('first.');
    })
    .then((secondRes) => {
      console.log('second.');
    });

  middleChain.then((thirdRes) => {
    console.log('third.');
  });

  // Regardless of position of PassivePromise instance in the chained promises, they always resolve root promise in the chain.
  middleChain.resolve(1);

  await middleChain; // first. second. third.
  
  // If you want to resolve the instance itself rather than root instance, use passiveResolve() and passiveReject() instead.

  const anotherChain = new PassivePromise(() => {})
    .then((firstRes) => {
      console.log('first.');
    })
    .then((secondRes) => {
      console.log('second.');
    });

  anotherChain.then((thirdRes) => {
    console.log('third.');
  });

  // We want to directly resolve this promise rather than the whole promise chain here.
  anotherChain.passiveResolve(1);

  await anotherChain; // third.
})()