promise-conditional
Use if-then-else in promise chains
Last updated 4 years ago by rstacruz .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install promise-conditional 
SYNC missed versions from official npm registry.

promise-conditional

Use if-then-else in promise chains

getArticles()
  .then(condition()
    .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 condition = require('promise-conditional')  // Legacy
import condition 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

condition

condition()

Starts a condition chain.

if

condition().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

condition() ... 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

condition() ... 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

condition() ... end()

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

then/catch/finally

condition().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

Current Tags

  • 1.2.0                                ...           latest (4 years ago)

3 Versions

  • 1.2.0                                ...           4 years ago
  • 1.1.0                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (2)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |