Giter Club home page Giter Club logo

promise-conditional's Introduction

promise-conditional

Use if-then-else in promise chains

getArticles()
  .then(conditional()
    .if(data => data.length > 10)
      .then(doIfTrue)
      .then(alsoDoIfTrue)
    .elseIf(data => data.length > 5)
      .then(doIfTrue)
    .else()
      .then(doIfFalse)
    .end())

Status

Install

npm install --save promise-conditional
var conditional = require('promise-conditional')  // Legacy
import conditional from 'promise-conditional'     // ES2015+

It relies on any-promise to find a Promise library. You can use bluebird, q, when, or the native Promise implementation of Node.js or the browser.

API

conditional

conditional()

Starts a condition chain. This is exported via require('promise-conditional').

if

conditional().if(condition)

Starts an if step. condition is a function that gets the input value. If it returns true, the subsequent then/catch/finally calls will be invoked.

elseIf

conditional() ... elseIf(condition)

Starts an elseIf step. condition is a function that gets the input value. If it returns true, the subsequent then/catch/finally calls will be invoked, but only if the other if steps have not been invoked.

else

conditional() ... else()

Starts an else step. The subsequent then/catch/finally calls will be invoked, but only if the other if/elseIf steps have not been invoked.

end

conditional() ... end()

Returns a function that you can pass onto .then(...) that runs the entire chain.

then/catch/finally

conditional().if().then(next)

Adds a then/catch/finally step if the last if condition is true.

Thanks

promise-conditional © 2016+, Rico Sta. Cruz. Released under the MIT License.
Authored and maintained by Rico Sta. Cruz with help from contributors (list).

ricostacruz.com  ·  GitHub @rstacruz  ·  Twitter @rstacruz

promise-conditional's People

Contributors

rstacruz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

promise-conditional's Issues

Handling Errors

Hi,
What happens if an error occurs in one of thens inside condition()?
Would it be delivered to the parent catch or an internal catch is required?
Thanks

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.