liar
v0.6.0
Published
A modular collection of tools for asynchronous programing via promises
Downloads
18
Readme
liar
A modular collection of tools for asynchronous programing via promises, all tools are available as a bundle here or standalone in their own repo.
API
npm install liar
var promise = require('liar');
promise(function(resolve,reject){
resolve(value);
//or
reject(reason);
});
a shortcut to my library lie. You need to give it a function which takes 2 arguments, a function to call on success and one to call on failure.
###all
promise.all(array (or promise for an array) of promises)
returns a promise for an array of all the responses, returns an error if any of the promises throw errors. Returned values are in the same order as the input array.
###some
promise.some(array (or promise for an array) of promises)
Similar to all but will only throw an error if all of the promises throw errors, otherwise returns an array of whichever values succeeded in the order that they completed, on error returns an array of errors.
###map
promise.map(array (or promise for an array) of promises, function)
Takes the array of values, applies function to them, and returns a promise for all the values. Function will be called with a value (not a promise) and may return either a promise or a value, array can filled with promises or values or a mixture.
###race
promise.race(array (or promise for an array) of promises)
resolved with whatever value or error that resolves first.
###cast
promise.cast(value or promise)
If it's a promise, returns it, if it's a value, returns a promise that resolves to it.
###use
promise.use(value or promise, function)
If the value is a promise, apply the function to the value it resolves into and return a promise for that, otherwise apply the function to the value and return the result.
###resolve
promise.resolve(value)
create a promise that is resolved with this value.
###reject
promise.reject(value)
create a promise that is rejected with this value.
###denodify
promise.denodify(function)
takes as an argument a function which has a callback as it's last argument, returns a function that acts identically except it returns a promise instead of taking a callback.
###fold left
promise.lfold(array (or promise for an array) of things,function,accumulator)
like Array.prototype.reduce, but the array may include promises or values and the function may return a promise or a value. promise.lfold
always return a promise.
###fold right
promise.rfold(array (or promise for an array) of things,function,accumulator)
like Array.prototype.reduceRight, but the array may include promises or values and the function may return a promise or a value. promise.rfold
always return a promise.
in other words it's like fold left but starts at the right
###fold
promise.fold(array (or promise for an array) of things,function,accumulator)
like Array.prototype.reduce, but the array may include promises or values and the function may return a promise or a value. promise.fold
always return a promise.
unlike lfold and rfold fold calls the values in the order the promises resolve.
###apply
promise.apply(function, one or more values or promises)
calls the function with the values or promises once they all resolve, returns the result.
###zip
promise.zip(one or more arrays of things (or promises for arrays))
promise.zips the 2 or more arrays up such that zip([1,2],['a','b'])
returns [[1,'a'],[2,'b']];
. When called with one array it is equivalent to lie-map called with the function(a){return [a]}
.
###zipwith
promise.zipwith(function,one or more arrays of things (or promises for arrays)
zips the 2 or more arrays up with a function 'func' such that zip(func(1,2),func('a','b'))
returns [func(1,'a'),func(2,'b')];
. When called with one array it is equivalent to lie-map (with the arguments in reverse order).
###filter
promise.filter(array (or promise for an array), function)
returns an array filtered based on the function, aka only truthy values are returned.
###every
promise.every(array (or promise for an array)[, function])
Applies the function the the array of promise or values (or mix) and returns true if they are all truthy.
It is lazy and will resolve as soon as the first falsy value is encountered.
If the function is omitted then it tests the truthiness of the values.
###any
promise.any(array (or promise for an array)[, function])
Applies the function the the array of promise or values (or mix) and returns true if at least one value is truthy.
It is lazy and will resolve as soon as one value returns true, if the function is omitted then it tests the truthiness of the array.
###iter
promise.iter(array of values or promises (or promise for such)[, function])
call the function on each value waiting for it to resolve before calling the next one. Returns an array of the values produced or the first error. If function is omitted it just resolves the promises sequentially (aka default function is function(a){return a}
)
###parallel
promise.parallel(array of values or promises (or promise for such)[, number, function])
A cross between lie-iter and lie-map, resolves the promises in parallel but only n of them at a time. Omitting the number is equivalent to map and setting it to 1 is the equivalent of lie-iter. Returns an array of the values produced or the first error. If function is omitted it just resolves the promises sequentially (aka default function is function(a){return a}
)
License
MIT