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-holder

v1.0.2

Published

Better promise manipulation

Downloads

18

Readme

promise-holder

npm version Build Status

Overview

promise-holder is a javascript/typescript package designed to simplify the handling of promises in your application. It provides a set of utilities and functions to streamline the creation, resolution, and rejection of promises, making asynchronous code management more efficient.

Installation

Install the package using npm:

npm install promise-holder

TL; DR

import PromiseHolder from 'promise-holder';

function waitTimeout() {
  const promise = new PromiseHolder();

  setTimeout(
    () => {
      // You can resolve you promise whenever you want
      promise.resolve(123);
      console.log('resolved');
    },
    100,
  )

  console.log('before await');

  // Promise can be awaited like regular promise
  const value = await promise;

  console.log('awaited', value);
}

await waitTImeout();
// before await
// resolved
// awaited 123

API

new PromiseHolder()

To create a promise, simply instantiate PromiseHolder class.

import PromiseHolder from 'promise-holder';

const promise = new PromiseHolder();

promise.then(onFulfilled, onRejected)

See Promise.then documentation.

promise.catch(onRejected)

See Promise.catch documentation.

promise.createdAt: date

Date at which the promise has been created

promise.resolve(value)

Callback to call to resolve the promise:

const promise = new PromiseHolder();

promise.resolve(123);

promise.then(
  (value) => {
    console.log(value); // 123
  }
);

Notice: calling the resolve method several more times will have no effect : only the first call will be resolved by the promise.

promise.reject(reason)

Callback to call to reject the promise:

const promise = new PromiseHolder();

const err = new Error('Promise rejected');
promise.reject(err);

promise.catch(
  (err) => {
    console.log(err.message); // Promise rejected
  }
);

Notice: calling the reject method several more times will have no effect : only the first call will be used as promise rejection reason.

promise.nodeCallback(err, value)

Use this method as node callback to resolve or reject the promise.

const PromiseHolder = require('promise-holder');
const fs = require('fs');

const promise = new PromiseHolder();
fs.readFile('myFile.txt', promise.nodeCallback);

promise.then(
  (file) => {
    console.log(file); // myFile.txt content
  },
);

promise.onTime(timeout): Promise<boolean>

Return a promise that indicate if the promiseHolder has been resolved/rejected before the indicated duration:

  • If the PromiseHolder has been resolved before the duration, then the promise resolve true. It will be resolved the moment the PromiseHolder has been fulfilled
  • If the PromiseHolder hasn't been resolved in time, the promise resolve false at the end of the duration.
const PromiseHolder = require('promise-holder');
const fs = require('fs');

const promise = new PromiseHolder();
promise.inTime(1000).then(
  (inTime) => {
    console.log('inTime', inTime); // inTime false
  }
)