q-combinators
Functions to combine q promises, capturing lots of useful, real world patterns used across Beamly's node.js services.
Installing
npm install q-combinators --save
API
.setPromiseImpl
All API methods below accept Q promises or any Promise/A+ implementation (e.g. bluebird or Native Promises). For backwards comptability q-combinators will use the 'Q' library internally and return Q promises by default. However if your project uses another promise implementation you can pass it to setPromiseImpl() to change this behaviour.
// use native promisesqCombinators; // use bluebird promisesqCombinators;
.object.all
Resolves an object of promises with an object of the resultant values if all promises resolve. If any promise rejects, it rejects with the same reason
// happy pathqCombinatorsobjectall x: y: z: ; // sad pathqCombinatorsobjectall x: Q y: z: ;
.object.allSettled
Resolves an object of promises with all results, using the same format as Q.allSettled
qCombinatorsobject;
.object.fulfilled
Resolves an object of promises with only the fulfilled values. If none of the promises fulfill, it fulfills with an empty object.
qCombinatorsobject;
.object.rejected
Resolves an object of promises with only the rejected values. If none of the promises are rejected, it fulfills with an empty object.
qCombinatorsobject;
.object.demand
Resolves an object of promises when the 'demanded' keys contain successful promises.
If a demanded promise fails, the returned promise will also fail.
// happy pathqCombinatorsobject; // sad path;
.array.fulfilled
Resolves an array of promises with only the fulfilled values. If none of the promises are fulfilled, it fulfills with an empty array.
qCombinatorsarray;
.array.rejected
Resolves an array of promises with only the rejected values. If none of the promises are rejected, it fulfills with an empty array.
qCombinatorsarray;
.chain
Sequentially executes an array of promise-returning functions. The equivalent of a lot of .then
chains:
var { return a + 1 };var { return }; qCombinators ;
.compose
Composes promise-producing functions into a single promise-producing function. Composes conventionally, from right to left.
In case of failure, returns the first failing promise in order of execution.
var { return };var { return }; var doubleThenAddTwo = qCombinators; ;
.fallback
Sequentially executes an array of functions which return promises, until the first promise is resolved. If all promises are rejected it itself is rejected with an array of all the failure reasons.
// happy pathqCombinators; // sad pathqCombinators;
.fallbackParallel
Same as .fallback, but takes an array of promises, allowing fetching results in parallel, then accepting them in preferential order.
// happy pathqCombinators; // sad pathqCombinators;
Contributing
Contributions are currently not being accepted.
Licensing
This project is licensed under the BSD 3-Clause license.