1 |
9bb1e829
|
cagy
|
'use strict';
|
2 |
|
|
|
3 |
|
|
Object.defineProperty(exports, "__esModule", {
|
4 |
|
|
value: true
|
5 |
|
|
});
|
6 |
|
|
exports.default = reflect;
|
7 |
|
|
|
8 |
|
|
var _initialParams = require('./internal/initialParams');
|
9 |
|
|
|
10 |
|
|
var _initialParams2 = _interopRequireDefault(_initialParams);
|
11 |
|
|
|
12 |
|
|
var _slice = require('./internal/slice');
|
13 |
|
|
|
14 |
|
|
var _slice2 = _interopRequireDefault(_slice);
|
15 |
|
|
|
16 |
|
|
var _wrapAsync = require('./internal/wrapAsync');
|
17 |
|
|
|
18 |
|
|
var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
|
19 |
|
|
|
20 |
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
21 |
|
|
|
22 |
|
|
/**
|
23 |
|
|
* Wraps the async function in another function that always completes with a
|
24 |
|
|
* result object, even when it errors.
|
25 |
|
|
*
|
26 |
|
|
* The result object has either the property `error` or `value`.
|
27 |
|
|
*
|
28 |
|
|
* @name reflect
|
29 |
|
|
* @static
|
30 |
|
|
* @memberOf module:Utils
|
31 |
|
|
* @method
|
32 |
|
|
* @category Util
|
33 |
|
|
* @param {AsyncFunction} fn - The async function you want to wrap
|
34 |
|
|
* @returns {Function} - A function that always passes null to it's callback as
|
35 |
|
|
* the error. The second argument to the callback will be an `object` with
|
36 |
|
|
* either an `error` or a `value` property.
|
37 |
|
|
* @example
|
38 |
|
|
*
|
39 |
|
|
* async.parallel([
|
40 |
|
|
* async.reflect(function(callback) {
|
41 |
|
|
* // do some stuff ...
|
42 |
|
|
* callback(null, 'one');
|
43 |
|
|
* }),
|
44 |
|
|
* async.reflect(function(callback) {
|
45 |
|
|
* // do some more stuff but error ...
|
46 |
|
|
* callback('bad stuff happened');
|
47 |
|
|
* }),
|
48 |
|
|
* async.reflect(function(callback) {
|
49 |
|
|
* // do some more stuff ...
|
50 |
|
|
* callback(null, 'two');
|
51 |
|
|
* })
|
52 |
|
|
* ],
|
53 |
|
|
* // optional callback
|
54 |
|
|
* function(err, results) {
|
55 |
|
|
* // values
|
56 |
|
|
* // results[0].value = 'one'
|
57 |
|
|
* // results[1].error = 'bad stuff happened'
|
58 |
|
|
* // results[2].value = 'two'
|
59 |
|
|
* });
|
60 |
|
|
*/
|
61 |
|
|
function reflect(fn) {
|
62 |
|
|
var _fn = (0, _wrapAsync2.default)(fn);
|
63 |
|
|
return (0, _initialParams2.default)(function reflectOn(args, reflectCallback) {
|
64 |
|
|
args.push(function callback(error, cbArg) {
|
65 |
|
|
if (error) {
|
66 |
|
|
reflectCallback(null, { error: error });
|
67 |
|
|
} else {
|
68 |
|
|
var value;
|
69 |
|
|
if (arguments.length <= 2) {
|
70 |
|
|
value = cbArg;
|
71 |
|
|
} else {
|
72 |
|
|
value = (0, _slice2.default)(arguments, 1);
|
73 |
|
|
}
|
74 |
|
|
reflectCallback(null, { value: value });
|
75 |
|
|
}
|
76 |
|
|
});
|
77 |
|
|
|
78 |
|
|
return _fn.apply(this, args);
|
79 |
|
|
});
|
80 |
|
|
}
|
81 |
|
|
module.exports = exports['default'];
|