promise-mix
Version:
Node module with some useful Promise composition functionalities.
87 lines (81 loc) • 3.24 kB
JavaScript
;
require("./promise-mix");
require("./promise-mix-logical");
const PromiseMux = require("./promise-mix-mux");
const Operation = require('./operation');
const baseFunctions = require('./config').base_functions;
const logicalFunctions = require('./config').logical_functions;
/**
* This simply adds concat version of base mix functions which can be chained after a promise and
* use the downstream as init value.
*/
for (let f in baseFunctions) {
const functionName = baseFunctions[f];
Promise.prototype[`_${functionName}`] = function (config) {
return this.then(prev => Promise[functionName](config, prev));
};
PromiseMux.prototype[`_${functionName}`] = function (config) {
return this.then(prev => Promise[functionName](config, prev));
};
}
/**
* This simply adds concat version of logical mix functions which can be chained after a promise and
* use the downstream as init value.
*/
for (let f in logicalFunctions) {
const functionName = logicalFunctions[f];
Promise.prototype[`_${functionName}`] = function (promisesFunctionsArray, check) {
return Promise[functionName]([() => this].concat(promisesFunctionsArray), check);
};
}
/**
* Executes a given function returning a promise inserting the result in the current
* promise chain, but discards any results from that Promise, keeping the original
* downstream.
* This is useful to execute async side-operations which should not influence the downstream.
* You can also disregard any errors in the aside promise by setting ignoreErros to true.
*
* @param {*} asidePromiseOperation an operation to execute aside from the downstream.
* @param {Boolean} ignoreErrors a flag telling the function to also ignore rejects of the aside function.
*
* Examples:
*
* Promise.resolve('Jake')
* ._aside((dwn) => Promise.resolve('Wolly'))
* .then((dwn) => {
* // Here dwn should equal 'Jake'
* });
*
* Promise.resolve('Jake')
* ._aside((dwn) => Promise.reject('Wolly'), true)
* .then((dwn) => {
* // Here dwn should equal 'Jake' and the main Promise should still be going.
* });
*/
Promise.prototype._aside = function (asidePromiseOperation, ignoreErrors = false) {
const operation = new Operation(asidePromiseOperation);
return this.then((result) => {
let res = operation.get(result);
if (!(res instanceof Promise)) res = Promise.resolve(eval);
return res.then((/*don't care!*/) => result)
.catch((error) => {
if (ignoreErrors) {
return Promise.resolve(result);
} else {
throw error;
}
});
});
};
/**
* Builds the downstream merging the fealds of the original downstraeam with those of the mixOperation
* downstream.
*
* @param {Operation} mixOperation the operation whose downstream you want to mix with the original downstream
* @returns {Promise} a Promise emitting downstream an object with mixed fields from original and mixed downstream.
*/
Promise.prototype._mix = function (mixOperation) {
const operation = new Operation(mixOperation);
return this.then(res => operation.get(res)
._aggregate(res));
}