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

enhanced-promises

v1.0.5

Published

add Q functionality to native JS promises (and polyfill Promises for old versions of JS)

Downloads

15

Readme

Build Status

What is this?

As you might know, Javascript Harmony introduces native Promises. However, these Promises lack useful functionality found in promise libraries such as Q. Also, native Promises aren't available in every environment. This library adds useful functionality to native promises such as spread, delay, makeNodeResolver, $promise, fcall, all(), nbind, ninvoke, nfcall, nfapply, denodify, nbind and npost.

Additionally, if native promises are not available, the library includes a very simple Promise implementation as a substitute.

You can just require('enhanced-promises') within node, or include enhanced-promises.min.js in your web project and start using native JS promises with all these useful methods.

What's the point?

I wanted to use native JS Promises, I wanted spread and delay from Q, and I didn't want to special case old browsers / versions of node that don't support promises.

How do I use this?

npm install enhanced-promises --save and include require('enhanced-promises') and carry on using new Promise(...) as you would before, with the added methods sprinked on top. If you're in a browser then include enhanced-promises.min.js

What are Promises?

So as I said, this is a Promise library. Ideally your runtime environment already supports promises, but if not this library provides a fallback. (from firefox documentation): The Promise object is used for deferred and asynchronous computations. A Promise represents an operation that hasn't completed yet, but is expected in the future. Learn more about promises, see more about promises at https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise

You can create promises in numerous ways, a few examples include:

new Promise(function(resolve, reject) {
  setTimeout(function(){ resolve(123) }, 1000);
}).then(function(value) {
  console.log(value); //123
});

or

Promise.fcall(function() {
  return 123;
}).then(function(value) {
  console.log(value); //123
});

or

Promise.resolve(123).then(function(value) {
  console.log(value); //123
});

or

Promise.reject(123).catch(function(value) {
  console.log(value); //123
});

Promise.delay(ms)

returns a promise that simply delays for X ms and then resolves

Promise
  .delay(1000)
  .then(function() {
    console.log('this prints after 1 second');
  })

Promise.fcall

You can create a promise from a value using Promise.fcall. This returns a promise for 10.

return Promise.fcall(function () {
    return 10;
});
You can also use fcall to get a promise for an exception.

return Promise.fcall(function () {
    throw new Error("Can't do it");
});

Defer

You can use deferred objects like so:

var deferred = Promise.defer();
FS.readFile("foo.txt", "utf-8", function (error, text) {
    if (error) {
        deferred.reject(new Error(error));
    } else {
        deferred.resolve(text);
    }
});
return deferred.promise;

$promise

This is sort of adapting NodeJS but I'm giving it its own section since I think it's cool. Objects have a $promise method on them so that you don't ned to wrap anything around a Promise call explicitly, you can simply do:

Fs.$promise('readFile', 'foo.txt').then(function(text){ ... });

The $promise function is available on any object and is the easiest way to turn a non-promise function into a function that returns a promise.

Adapting NodeJS

You can also wrap promises around nodejs functionality using:

Promise.nfcall(FS.readFile, "foo.txt", "utf-8").then(function(text){ ... });
Promise.nfapply(FS.readFile, ["foo.txt", "utf-8"]).then(function(text){ ... });

If you want to invoke a function belonging to an object without changing the value for this you can do:

Promise.ninvoke(redisClient, "get", "user:1:id");
Promise.npost(redisClient, "get", ["user:1:id"]);

and you can make re-usable versions of the above like so:

var readFile = Promise.denodeify(FS.readFile);
return readFile("foo.txt", "utf-8");

var redisClientGet = Promise.nbind(redisClient.get, redisClient);
return redisClientGet("user:1:id");

You can also use makeNodeResolver:

var deferred = Promise.defer();
FS.readFile("foo.txt", "utf-8", deferred.makeNodeResolver());
return deferred.promise;

Spread

use as a replacement for then, if the previous promise returned an array, spread treats each element as a separate property, for example:

var getUsername = function() { .. returns some random promise .. }

Promise
  .all([
    123,
    Promise.resolve(456)
    getUsername()
  ])
  .spread(function(firstNumber, secondNumber, username) {
    console.log('woohoo!');
  })