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

or-promise

v1.0.0

Published

Easily support returning a promise when a callback is omitted

Downloads

27

Readme

or-promise

NPM version Build Status Coverage Status

Accept a callback OR return a promise (easily).

example

const orPromise = require('or-promise')

function greetSoon (cb) {
    cb = orPromise(cb)
    setImediate(cb, null, 'hello')

    // this will return undefined if cb was provided, otherwise
    // it will return a promise
    return cb.promise
}

// we can now use our fancy function either way...
greetSoon(function (err, msg) {
    if (err) return console.error(err)
    console.log(msg)
})

// or a promise
greetSoon().then(console.log).catch(console.error)

example with default parameters

Same function as above, but using default parameters (if your JS engine supports it):

const orPromise = require('or-promise')

function greetSoon (cb = orPromise()) {
    setImediate(cb, null, 'hello')
    return cb.promise
}

api

var orPromise = require('or-promise')

cb = orPromise([cb])

When called with a (truthy) cb argument, orPromise simply returns the argument it was called with, unaltered.

If called with no argument, a function will be returned that is usable as an error-first callback. This function will have a promise property. When the returned callback is invoked, the promise property will be:

  • resolved if the callback was invoked with a falsy first argument
  • rejected if the callback was invoked with a truthy first argument

If resolved, the resolved value will be the 2nd argument supplied to the callback. If rejected, the error will be the 1st argument supplied to the callback.

testing

npm test

browser test

npm run browser-test

This will run the tests in all browsers (specified in .zuul.yml). Be sure to educate zuul first.

coverage

npm run open-cover

Open coverage in browser.