Back to Async

reflectAll.js

docs/v2/reflectAll.js.html

3.2.62.0 KB
Original Source
import reflect from './reflect';
import isArray from 'lodash/isArray';
import _arrayMap from 'lodash/_arrayMap';
import forOwn from 'lodash/_baseForOwn';

/**
 * A helper function that wraps an array or an object of functions with `reflect`.
 *
 * @name reflectAll
 * @static
 * @memberOf module:Utils
 * @method
 * @see [async.reflect]{@link module:Utils.reflect}
 * @category Util
 * @param {Array|Object|Iterable} tasks - The collection of
 * [async functions]{@link AsyncFunction} to wrap in `async.reflect`.
 * @returns {Array} Returns an array of async functions, each wrapped in
 * `async.reflect`
 * @example
 *
 * let tasks = [
 * function(callback) {
 * setTimeout(function() {
 * callback(null, 'one');
 * }, 200);
 * },
 * function(callback) {
 * // do some more stuff but error ...
 * callback(new Error('bad stuff happened'));
 * },
 * function(callback) {
 * setTimeout(function() {
 * callback(null, 'two');
 * }, 100);
 * }
 * ];
 *
 * async.parallel(async.reflectAll(tasks),
 * // optional callback
 * function(err, results) {
 * // values
 * // results[0].value = 'one'
 * // results[1].error = Error('bad stuff happened')
 * // results[2].value = 'two'
 * });
 *
 * // an example using an object instead of an array
 * let tasks = {
 * one: function(callback) {
 * setTimeout(function() {
 * callback(null, 'one');
 * }, 200);
 * },
 * two: function(callback) {
 * callback('two');
 * },
 * three: function(callback) {
 * setTimeout(function() {
 * callback(null, 'three');
 * }, 100);
 * }
 * };
 *
 * async.parallel(async.reflectAll(tasks),
 * // optional callback
 * function(err, results) {
 * // values
 * // results.one.value = 'one'
 * // results.two.error = 'two'
 * // results.three.value = 'three'
 * });
 */
export default function reflectAll(tasks) {
    var results;
    if (isArray(tasks)) {
        results = _arrayMap(tasks, reflect);
    } else {
        results = {};
        forOwn(tasks, function(task, key) {
            results[key] = reflect.call(this, task);
        });
    }
    return results;
}