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

promise-helpers

v0.2.0

Published

Some helpers for using promises with node

Downloads

5

Readme

promise-helpers

Some helpers for using promises with node. The helpers are inspired by Q.

Note

The helpers extend the global Promise function and prototype. For this to work, the Promise function must exist and match the ES6 Promise spec.

To add a shim for a valid spec, use npm modules like es6-shim or es6-promise.

Extensions

Promise#finally(fn)

fn is called at the specified point, regardless of the resolved or rejected state of the promise.

The promise returned will have the same resolved or rejected state as before fn was called.

See https://github.com/matthew-andrews/Promise.prototype.finally for details.

Promise#spread(okHandler[, errorHandler])

An alternative to then(). If the promise resolves to an array, the okHandler is called with the contents as parameters. This makes it very convenient to use with Promise.all().

Promise.all([ firstPromise, secondPromise ])
    .spread(function(firstResult, secondResult) {
        ...
    })

is equal to

Promise.all([ firstPromise, secondPromise ])
    .then(function(results) {
        var firstResult = results[0]
        var secondResult = results[1]
        ...
    })

It will also pass the array through Promise.all(), so the above example is also equal to

Promise.resolve([ firstPromise, secondPromise ])
    .spread(function(firstResult, secondResult) {
        ...
    })

Promise#all()

A chainable convenience for Promise.all().

somePromise
    .then(function(values) {
        return values.map(transformThatReturnsPromises)
    })
    .all()
    .then(handleResults)

is equivalent to

somePromise
    .then(function(values) {
        return values.map(transformThatReturnsPromises)
    })
    .then(function(results) { return Promise.all(results) })
    .then(handleResults)

Promise#nodeify(fn)

A helper to bridge a promise-based API to node-style functions.

If a function is passed to the nodeify() function, it will be called when the promise resolves, with the error as the first parameter.

If no function is passed in, it returns the promise directly.

function someFunction(args, cb) {
    return doWork(args).nodeify(cb)
}

is equal to

function someFunction(args, cb) {
    var promise = doWork(args)
    if(!cb) return promise

    promise.then(function(result) { cb(result) }, cb)
}

Promise#done()

Caps a promise chain. No promise is returned from this call.

If the promise would be rejected, done() will rethrow the error in global scope.

Promise.nfapply(fn, args)

A convenience method for wrapping a function expecting a Node-style callback in a promise.

It unfolds the args array as parameters for the fn param, and appends a callback on the end.

If the first parameter is set, the promise is rejected with the error.

If the first parameter is not set, the promise is resolved with an array containing the other parameters.

If no arguments are supplied on a success-case, the promise will resolve to an empty array.

Promise.nfcall(fn[, arg1, ...])

A variant of Promise.nfapply(), where the arguments are given one-by-one. Think function#call() and function#apply().

Promise.denodeify(fn, options)

Returns a Promise-wrapped version of fn. When called, it will transfer the arguments + a callback to the original function, and resolve the promise when the original is done.

It is a convenience method around Promise.nfapply.bind(null, fn).

It can take an options, singleResult, which, when true, will always return the first result of the result-array that nfapply returns.

This means that for no results, the first param will be undefined, and for multiple results, only the first is passed along.