Skip to content

aznick/bluebird-promisell

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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'

About

A practical functional programming library for promises

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%