Giter Site home page Giter Site logo

bluebird-promisell's Introduction

bluebird-promisell

General

Functor, Applicative, Traversable, Foldable instances for bluebird Promise

Check out the API docs

Build Status npm version dependecies

API

Takes any value, returns a resolved Promise with that value

> purep(3).then(console.log)
promise
3

Transforms a Promise of value a into a Promise of value b

> fmapp(function(a) { return a + 3; }, Promise.resolve(4)).then(console.log)
promise
7

Transforms an array of Promise of value a into a Promise of array of a.

> sequencep([Promise.resolve(3), Promise.resolve(4)]).then(console.log)
promise
[3, 4]

Maps a function that takes a value a and returns a Promise of value b over an array of value a, then use sequencep to transform the array of Promise b into a Promise of array b

> traversep(function(a) { return Promise.resolve(a + 3); })(
    [2, 3, 4])
promise
[5, 6, 7]

Performs left-to-right composition of an array of Promise-returning functions.

> pipep([
    function(a) { return Promise.resolve(a + 3); },
    function(b) { return Promise.resolve(b * 10); },
  ])(6);
promise
90

Takes a function so that this function is able to read input values from resolved Promises, and return a Promise that will resolve with the output value of that function.

> liftp(function(a, b, c) { return (a + b) * c; })(
    Promise.resolve(3),
    Promise.resolve(4),
    Promise.resolve(5));
promise
35

Takes a function and apply this function to the resolved Promise value, and return a Promise that will resolve with the output of that function.

> liftp1(function(user) {
    return user.email;
  })(Promise.resolve({ email: '[email protected]' }));
promise
abc@example.com

Takes two Promises and return the first if both of them are resolved alias <* firstp

> firstp(Promise.resolve(1), Promise.resolve(2))
promise
1

> firstp(Promise.resolve(1), Promise.reject(new Error(3)))
promise
Error 3

Takes two Promises and return the second if both of them are resolved alias *> secondp

> secondp(Promise.resolve(1), Promise.resolve(2))
promise
2

> secondp(Promise.resolve(1), Promise.reject(new Error(3)))
promise
Error 3

Takes a predicat that returns a Promise and an array of a, returns a Promise of array a which satisfy the predicate.

> filterp(function(a) { return Promise.resolve(a > 3); })([2, 3, 4])
promise
[4]

Returns a Promise of value b by iterating over an array of value a, successively calling the iterator function and passing it an accumulator value of value b, and the current value from the array, and then waiting until the promise resolved, then passing the result to the next call.

foldp resolves promises sequentially

> foldp(function(b, a) { return Promise.resolve(b + a); })(1)([2, 3, 4])
promise
10

Transform the rejected Error.

> mapError(function(err) {
    var newError = new Error(err.message);
    newError.status = 400;
    return newError;
  })(Promise.reject(new Error('Not Found')));
rejected promise

Recover from a rejected Promise

> resolveError(function(err) {
    return false;
  });

promise false

Takes a predict function and a toError function, return a curried function that can take a value and return a Promise. If this value passes the predict, then return a resolved Promise with that value, otherwise pass the value to the toError function, and return a rejected Promise with the output of the toError function.

var validateGreaterThan0 = toPromise(function(a) {
  return a > 0;
}, function(a) {
  return new Error('value is not greater than 0');
});

> validateGreaterThan0(10)
promise
10

> validateGreaterThan0(-10)
rejected promise
Error 'value is not greater than 0'

bluebird-promisell's People

Contributors

zhangchiqing 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.