Giter VIP home page Giter VIP logo

ethr's Introduction

Either: Promise.finally for ES6

npm Build Status Codecov NodeJS Version dependency count package size

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.

ethr's People

Contributors

codelenny avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.