source: trip-planner-front/node_modules/async/reflect.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.3 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = reflect;
7
8var _initialParams = require('./internal/initialParams');
9
10var _initialParams2 = _interopRequireDefault(_initialParams);
11
12var _slice = require('./internal/slice');
13
14var _slice2 = _interopRequireDefault(_slice);
15
16var _wrapAsync = require('./internal/wrapAsync');
17
18var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
19
20function _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 */
61function 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}
81module.exports = exports['default'];
Note: See TracBrowser for help on using the repository browser.