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

controlled-promise

v0.2.0

Published

Better control of JavaScript promises

Downloads

3,493

Readme

controlled-promise

Build Status npm license

Better control of JavaScript Promises

A Promise wrapping library for advanced control of promise lifecycle. Allows to split business logic from promise manipulation:

  • promisify event-emitters: easy access to resolve / reject callbacks
  • return existing promise while it is pending
  • auto-reject after configured timeout
  • tiny size and zero dependencies

Installation

npm install controlled-promise --save

Usage

const ControlledPromise = require('controlled-promise');

// Create controlled promise
const cp = new ControlledPromise();

// Call asynchronous function. Returns promise wich `resolve / reject` callbacks are stored in `cp`.
const promise = cp.call(() => someAsyncFn());

// Resolve promise later via cp.resolve()
cp.resolve();

// OR reject promise later via cp.reject()
cp.reject(error);

API

ControlledPromise

Controlled Promise.

Kind: global class

new ControlledPromise()

Creates controlled promise. In contrast to original Promise, it does not immediately call any function. Instead it has .call() method for that and resolve / reject methods for resolving promise.

controlledPromise.promise ⇒ Promise

Returns promise itself.

Kind: instance property of ControlledPromise

controlledPromise.value ⇒ *

Returns value with that promise was fulfilled (resolved or rejected).

Kind: instance property of ControlledPromise

controlledPromise.isPending ⇒ Boolean

Returns true if promise is pending.

Kind: instance property of ControlledPromise

controlledPromise.isFulfilled ⇒ Boolean

Returns true if promise is fulfilled.

Kind: instance property of ControlledPromise

controlledPromise.isRejected ⇒ Boolean

Returns true if promise rejected.

Kind: instance property of ControlledPromise

controlledPromise.isSettled ⇒ Boolean

Returns true if promise fulfilled or rejected.

Kind: instance property of ControlledPromise

controlledPromise.isCalled ⇒ Boolean

Returns true if promise already called via .call() method.

Kind: instance property of ControlledPromise

controlledPromise.call(fn) ⇒ Promise

This method executes fn and returns promise. While promise is pending all subsequent calls of .call(fn) will return the same promise. To fulfill that promise you can use .resolve() / .reject() methods.

Kind: instance method of ControlledPromise

| Param | Type | | --- | --- | | fn | function |

controlledPromise.resolve([value])

Resolves pending promise with specified value.

Kind: instance method of ControlledPromise

| Param | Type | | --- | --- | | [value] | * |

controlledPromise.reject([value])

Rejects pending promise with specified value.

Kind: instance method of ControlledPromise

| Param | Type | | --- | --- | | [value] | * |

controlledPromise.reset()

Resets to initial state.

Kind: instance method of ControlledPromise

controlledPromise.timeout(ms, [reason])

Sets timeout to reject promise automatically.

Kind: instance method of ControlledPromise

| Param | Type | Description | | --- | --- | --- | | ms | Number | delay in ms after that promise will be rejected automatically | | [reason] | String | Error | function | rejection value. If it is string or error - promise will be rejected with that error. If it is function - this function will be called after delay where you can manually resolve or reject promise via .resolve() / .reject() methods. |

Related

License

MIT @ Vitaliy Potapov