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

node-promise

v0.5.14

Published

Kris Zyp's implementation of promises with added features, maintained as an npm package.

Downloads

32,261

Readme

MIT License.

The node-promise project provides a complete promise implementation. Promises provide a clean separation of concerns between asynchronous behavior and the interface so asynchronous functions can be called without callbacks, and callback interaction can be done on the generic promise interface. The node-promise package provides just a promise implementation, however, https://github.com/kriszyp/promised-io is recommended for more complete promise-based IO functionality. The promised-io includes the promise implementation from node-promise, as well as wrappers around Node's filesystem and other system I/O APIs for consistent promise-based interaction.

The node-promise module features a promise implementation with:

  • Chainable promises
  • Promises throw errors if an error handler is not provided
  • CommonJS promise proposal [1] compliant
  • Immutable once fulfilled to reduce possible side-effects
  • Promises can be used securely (as separate resolver/promise pairs in ocap situations)
  • Backwards compatibility where possible (addCallback, addErrback, emitSuccess, and emitError should still behave as expected)

Utility functions, including:

  • when() - Normalization of sync (normal values) and async (promises)
  • all() - Create a promise that accumulate multiple concurrent promises (failed promises resolve to Error objects)
  • allOrNone() - Ditto, but the first promise to fail causes the composition to fail as well
  • first() - Find the first promise to be fulfilled in a group of promises
  • seq() - Sequentially execute a set of promise returning functions
  • delay() - Returns a promise that is fulfilled after a given amount of time
  • execute() - Executes a function that takes a callback and returns a promise (thank you Benjamin Thomas for providing this)

Much of this is adapted from Tyler Close's ref_send and Kris Kowal's work on promises.

Some quick examples from test-promise.js (again, it is recommended that you use http://github.com/kriszyp/promised-io for file and other I/O interaction): util = require('util'); var fs = require('./fs-promise');

// open a file and read it
fs.open("fs-promise.js", process.O_RDONLY).then(function(fd){
  return fs.read(fd, 4096);
}).then(function(args){
  util.puts(args[0]); // print the contents of the file
});

// does the same thing
fs.readFile("fs-promise.js").addCallback(util.puts);

A default Promise constructor can be used to create a self-resolving deferred/promise:

var Promise = require("node-promise").Promise;
var promise = new Promise();
asyncOperation(function(){
  promise.resolve("succesful result");
});
promise -> given to the consumer

A consumer can use the promise:

promise.then(function(result){
   ... when the action is complete this is executed ...
},
function(error){
    ... executed when the promise fails
});

Alternately, a provider can create a deferred and resolve it when it completes an action. The deferred object a promise object that provides a separation of consumer and producer to protect promises from being fulfilled by untrusted code.

var defer = require("node-promise").defer;
var deferred = defer();
asyncOperation(function(){
  deferred.resolve("succesful result");
});
deferred.promise -> given to the consumer

Another way that a consumer can use promises:

var when = require("node-promise").when;
when(promise,function(result){
   ... when the action is complete this is executed ...
},
function(error){
   ... executed when the promise fails
});

More examples:

function printFirstAndLast(itemsDeferred){
  findFirst(itemsDeferred).then(util.puts);
  findLast(itemsDeferred).then(util.puts);
}
function findFirst(itemsDeferred){
  return itemsDeferred.then(function(items){
    return items[0];
  });
}
function findLast(itemsDeferred){
  return itemsDeferred.then(function(items){
    return items[items.length];
  });
}

And now you can do:

printFirstAndLast(someAsyncFunction());

The workhorse function of this library is the "when" function, which provides a means for normalizing interaction with values and functions that may be a normal synchronous value, or may be a promise (asynchronously fulfilled). The when() function takes a value that may be a promise or a normal value for the first function, and when the value is ready executes the function provided as the second argument (immediately in the case of a non-promise normal value). The value returned from when() is the result of the execution of the provided function, and returns a promise if provided a promise or synchronously returns a normal value if provided a non-promise value. This makes it easy to "chain" computations together. This allows us to write code that is agnostic to sync/async interfaces:

var when = require("node-promise").when;
function printFirstAndLast(items){
  // print the first and last item
  when(findFirst(items), util.puts);
  when(findLast(items), util.puts);
}
function findFirst(items){
   // return the first item
   return when(items, function(items){
     return items[0];
   });
}
function findLast(items){
   // return the last item
   return when(items, function(items){
     return items[items.length - 1];
   });
}

Now we can do:

> printFirstAndLast([1,2,3,4,5]);
1
5

And we can also provide asynchronous promise:

var promise = new process.Promise();
> printFirstAndLast(promise);

(nothing printed yet)

> promise.emitSuccess([2,4,6,8,10]);
2
10

The "all" function is intended to provide a means for waiting for the completion of an array of promises. The "all" function should be passed an array of promises, and it returns an promise that is fulfilled once all the promises in the array are fulfilled. The returned promise's resolved value will be an array with the resolved values of all of the promises in the passed in array.

The "first" function is intended to provide a means for waiting for the completion of the first promise in an array of promises to be fulfilled. The "first" function should be passed an array of promises, and it returns an promise that is fulfilled once the first promise in the array is fulfilled. The returned promise's resolved value will be the resolved value of the first fulfilled promise.