prominator

1.3.0 • Public • Published

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 (& 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

Readme

Keywords

Package Sidebar

Install

npm i prominator

Weekly Downloads

0

Version

1.3.0

License

MIT

Last publish

Collaborators

  • bakerface