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

norman-promise

v0.3.0

Published

ECMAScript 6 compliant Promise implementation with a few nice extensions

Downloads

5

Readme

norman-promise

ECMAScript 6 compliant Promise implementation with a few nice extensions.

#API

##Creating Promises

###Standard Promise API

var p = new Promise(function (resolve, reject) {
    // Do something async and eventually call resolve or reject
});

###Defer API

var p = Promise.defer(); // Underlying promise is p.promise

// Do something async and eventually call p.resolve or p.reject

##Promise.prototype methods ###then(function: onFulfilled, function: onRejected)

var p = new Promise(function (resolve, reject { ... })
                .then(function (result) {
                    // this will be called if promise is fulfilled
                }, function (err) {
                    // this will be called if promise is rejected
                });

###catch(function: onRejected)

var p = new Promise(function (resolve, reject { ... })
                .catch(function (err) {
                    // this will be called if promise is rejected
                });

###Correctly catching errors

// Not recommanded: if something happen in the onFulfilled callback, onRejected will not be called
var p1 = new Promise(function (resolve, reject { ... })
                .then(onFulfilled, onRejected);
                
// Much better: if something happen in the onFulfilled callback, onRejected will be called
var p2 = new Promise(function (resolve, reject { ... })                 
                .then(onFulfilled)
                .catch(onRejected);

###finally(function: finalizer)

This promise extension allows having garbage collection code running without interfering with the promise chain

var p = doSomethingAsync1()
            .then(function (result) {
                return doSomethingAsync2(result);
            })
            .finally(function () {
                // you may put cleanup code here, it will always be called
            })
            .then (function (result2) {
                // result2 is the resolved value of doSomethingAsync2(result)
            });

###callback(function(Error, any): done) This promise extension allows supporting optional callbacks when promise is settled

function myFunctionWithOptionalCallback(done) {
  return doSomethingAsync().callback(done);
}
  • p.callback(done) returns a promise which will be settled after p is settled

  • If done is a function, done will be called when p is settled

  • Callback function follows standard node.js convention:

    • if promise is fulfilled, result is passed as second parameter

    • if promised is rejected, error is passed as first parameter

##Promise.prototype properties

###id: Number Unique promise identifier (could be useful for debugging)

###result: any Once the promise is settled this property returns the promise result or reject error

###status: The status of the promise, possible values are

  • Promise.PENDING

  • Promise.FULFILLED

  • Promise.REJECTED

##Promise constructor methods

###Promise.resolve(any: resolution) Returns a promise resolved with the given resolution. If the resolution is a Promise or a thenable object, the resulting promise will be settled when the resolution is settled and fulfilled or rejected accordingly.

###Promise.reject(Error: err) Returns a rejected promise with reason err.

###Promise.cast(any: wrapped) Returns a promise wrapping the wrapped input.

  • if wrapped is a Promise, Promise.cast returns wrapped itself

  • otherwise, Promise.cast is equivalent to Promise.resolve.

###Promise.all(Promise[]: promises) Returns a promise with the following properties.

  • It will be fulfilled if/when all promises in promises array are fulfilled. In that case, it is fulfilled with an array of the promises results.

  • it will be rejected if/when one the promises is rejected. In that case it is rejected with the reason of the rejected promise.

###Promise.race(Promise[]: promises) Returns a promise fulfilled/rejected with the result of the first promise to be settled.

###Promise.waitAll(Promise[]: promises) Returns a promise with the following properties

  • It will be settled when all promises are settled.

  • If all promises are fulfilled, Promise.all will be fulfilled with an array of the promise results

  • If at least one promise is rejected, Promise.all will be rejected with an error object with a detail property

    • detail.failCount contains the number of rejected promises

    • detail.errors is an array of promise reject errors: if k-th promise is rejected detail.errors[k] contains the corresponding error otherwise detail.errors[k] is undefined

    • detail.results is an array of promise results: if k-th promise is fulfilled detail.results[k] contains the corresponding result otherwise detail.result[k] is undefined

###Promise.invoke(function: fn, ...) Convert a standard node.js callback-based call to a promise-based one

var fs = require("fs");

var p = Promise.invoke(fs.open, "tempFile.txt", "r")
          .then(function (fd) {
            // If method returns a single value it is available as promise result
            var buffer = new Buffer(4096);
            return Promise.invoke(fs.read, fd, 0, 4096, 0)
                .then(function (result) {
                  // If method returns multiple values, the promise result wraps them in an array
                  var bytesRead = result[0];
                  var buf = result[1];
                });
          });