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

prominator

v1.3.0

Published

Combinators for your Promises

Downloads

8

Readme

prominator

npm package build code climate coverage issues dependencies devDependencies

Table of Contents

  • catchIf(predicate, fn) - catch an error if a condition is met
  • expect(...modifiers, predicate) - expect a condition to be met
  • expectCatch(err) - expect an error to be caught
  • Prominator.lift(fn, [instance]) - convert a node-style function to a promise
  • rethrow(err) - re-throw an error

catchIf(predicate, fn)

Catches an error and invokes fn if predicate returns a truthy value. If a falsy value is returned, the error will be re-thrown and the fn is not invoked.

const Prominator = require('prominator');

function instanceOf(Type) {
  return function (o) {
    return o instanceof Type;
  };
}

Prominator.resolve(new SyntaxError())
  .catchIf(instanceOf(TypeError), console.log)
  .catch(console.log); // => SyntaxError

Prominator.resolve(new SyntaxError())
  .catchIf(instanceOf(SyntaxError), console.log) // => SyntaxError
  .catch(console.log);

expect(...modifiers, predicate)

Expects that a condition is met. The resolved value of the promise is modified using the modifiers and then passed to a predicate. If the predicate returns a truthy value, then the resolved value of the promise is returned. If the predicate returns a falsy value, then the promise is rejected.

const Prominator = require('prominator');

function odd(n) {
  return (n & 1);
}

function plus(n) {
  return function (m) {
    return m + n;
  };
}

Prominator.resolve(3)
  .expect(odd)
  .then(console.log); // => 3

Prominator.resolve(4)
  .expect(odd)
  .catch(console.log); // => AssertionError

Prominator.resolve(6)
  .expect(plus(1), odd)
  .then(console.log); // => 6

Prominator.resolve(7)
  .expect(plus(1), odd)
  .catch(console.log); // => AssertionError

expectCatch(err)

Returns a new promise that resolves if err is caught. If no errors are caught or the error is not equal to err, then the promise is rejected.

const Prominator = require('prominator');

Prominator.resolve('foo')
  .expectCatch('error')
  .catch(console.log); // => ExpectedPromiseRejectionError

Prominator.reject('foo')
  .expectCatch('error')
  .catch(console.log); // => AssertionError

Prominator.reject('error')
  .expectCatch('error')
  .then(console.log); // => undefined

Prominator.lift(fn, [instance])

Converts the node-style function fn to a promise. If an instance is provided, it will be bound as the this parameter for the function.

const Prominator = require('prominator');

function add(n, callback) {
  callback(null, this + n);
}

const add3 = Prominator.lift(add, 3);
add3(4).then(console.log); // => 7

rethrow(err)

Catches an error and throws err in its place.

const Prominator = require('prominator');

Prominator.reject('foo')
  .rethrow('bar')
  .catch(console.log); // => bar

Prominator.resolve('foo')
  .rethrow('bar')
  .then(console.log); // => foo