pc

promise-conditional

Use if-then-else in promise chains

Showing:

Popularity

Downloads/wk

32

GitHub Stars

33

Maintenance

Last Commit

5yrs ago

Contributors

1

Package

Dependencies

2

Size (min+gzip)

1.3KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

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

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial