@xailabs/altx
Version:
Flux flavor based on alt.js
53 lines (47 loc) • 1.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = callSeries;
var _promise = require('promise');
var _promise2 = _interopRequireDefault(_promise);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Retuns a promise and executes a series of calls.
* Uses a timeout initially so that we don't run into errors when still in the middle of a dispatch.
*
* import {callSeries} from 'shared/flux';
*
* @param {array} calls - An array of function that each returns a promise
* @param {object} options - An array of function that each returns a promise
* @param {boolean} options.log - Whether to log individual calls
* @return {promise} - A promise that will be resolved when all calls succeeded or rejected if one call failed
*/
function callSeries(calls) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref$log = _ref.log,
log = _ref$log === undefined ? false : _ref$log;
return new _promise2.default(function (resolve, reject) {
var results = [];
setTimeout(function () {
function series(list) {
log && console.log('[callSeries] list', list);
var p = _promise2.default.resolve();
return list.reduce(function (pacc, fn) {
return pacc = pacc.then(function (res) {
log && console.log('[callSeries] res', res);
results.push(res);
return fn(res);
});
}, p);
}
series(calls).then(function () {
return setTimeout(function () {
return resolve(results);
});
}).catch(function (err) {
return reject(err);
});
});
});
}