0.0.1 • Published 7 years ago

ethr v0.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

Either: Promise.finally for ES6

npm Build Status Codecov NodeJS Version

A modified version of Bluebird's Promise.finally designed to fit into existing code.

Instead of modifying the entire Promise chain to add a .finally() method, Either takes advantage of the both callbacks supported by Promise#then to easily set both a success clause and a failure clause.

const either = require("ethr");

Promise
  .reject()
  .then(...either(() => {
    console.log("This will run.");
  }));

Either returns two callbacks: one to handle fulfilled promises, and one to handle rejected promises. ES6 Spread syntax (supported in Node 5.12.0+ and most modern browsers) can be used to quickly unpack these into the two sides of Promise#then, but is not required.

Once the Promise chain has gotten to Either, the callback passed to either() will always be called with the current state (true if promise is fulfilling, false if promise is rejected).

In general, the returned value from the callback is inserted into the Promise chain.

  • If any errors are thrown, or the callback returns a rejecting Promise, the error is passed along to the next Promise handler.
  • If the callback chain was previously rejecting and the callback returns a normal value or a fulfilling Promise, then the previous error will be re-thrown for the next Promise handler.
  • If the chain was previously resolved, then any returned value or resolved value from the callback will be passed on to the chain.

See the tests for specific examples.