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 🙏

© 2025 – Pkg Stats / Ryan Hefner

deferred-ap

v0.1.0

Published

A Deferred extension for any Promises/A+ compliant promise library.

Downloads

12

Readme

deferred-ap

A Deferred extension for any Promises/A+ compliant promise library.

Soap Box

Deferreds are somewhat stigmatized by the JavaScript Borg Collective as an anti-pattern. For a good discussion on the matter, see:

https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns#the-deferred-anti-pattern

While there are some specific cases where Deferreds are necessary it is important to understand that they should be used judiciously. Make sure you think carefully about your design if you discover that you "need" to use Deferreds. You may very well be correct, but you could also be creating code that other JS developers may find difficult to understand and contribute to (whether proprietary or open source).

Installation

npm install deferred-ap

Usage

Creating Instances

var Deferred = require('deferred-ap');

var deferred = new Deferred();

When creating new Deferred instances in this manner, the constructor will default to the Promise constructor defined in the global namespace. This works nicely if you're in an environment that, for instance, supports ES6 Promises natively. However you can run into issues if you're environment does not support Promises out of the box.

Custom Promise Constructors

deferred-ap allows you to override the default Promise constructor that it uses internally. This enables you to override the native ES6 Promise constructor or define a Promise constructor in the first place.

var Deferred = require('deferred-ap');
var Promise = require('bluebird').Promise;

var deferred = new Deferred(Promise);  // Uses the bluebird constructor.

You can also set a specific Promise constructor as the default internal constructor.

var Deferred = require('deferred-ap');
var Promise = require('bluebird').Promise;

Deferred.configure(Promise);

var deferred = new Deferred();  // Uses the bluebird constructor.

Resolution/Rejection

Deferred instances can be easily resolved or rejected.

Resolving:

var Deferred = require('deferred-ap');

var deferred = new Deferred();

deferred.resolve('foo');

Rejecting:

var Deferred = require('deferred-ap');

var deferred = new Deferred();

deferred.reject(new Error('bar'));

The functions .resolve() and .reject() function in exactly the same way as their counterparts in:

var promise = new Promise(function(resolve, reject) {
   ...
});

Determining State

deferred-ap allows you to check on the state of a particular instance. This can be moderately useful for dealing with Promise constructors like the native ES6 constructor that doesn't inherently allow state checks. The functionality is intended merely as a convenience and not as a replacement for semantic state checks in custom Promise implementations. Possible states are pending, resolved, and rejected. States returned as strings enables using switch statements around the state of the Deferred instance.

var Deferred = require('deferred-ap');

var deferred = new Deferred();

deferred.state;  // 'pending'

deferred.resolve(null);

deferred.state;  // 'resolved'