mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-24 09:25:15 +00:00
81 lines
2.3 KiB
JavaScript
81 lines
2.3 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.default = reflect;
|
||
|
|
||
|
var _initialParams = require('./internal/initialParams');
|
||
|
|
||
|
var _initialParams2 = _interopRequireDefault(_initialParams);
|
||
|
|
||
|
var _slice = require('./internal/slice');
|
||
|
|
||
|
var _slice2 = _interopRequireDefault(_slice);
|
||
|
|
||
|
var _wrapAsync = require('./internal/wrapAsync');
|
||
|
|
||
|
var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
/**
|
||
|
* Wraps the async function in another function that always completes with a
|
||
|
* result object, even when it errors.
|
||
|
*
|
||
|
* The result object has either the property `error` or `value`.
|
||
|
*
|
||
|
* @name reflect
|
||
|
* @static
|
||
|
* @memberOf module:Utils
|
||
|
* @method
|
||
|
* @category Util
|
||
|
* @param {AsyncFunction} fn - The async function you want to wrap
|
||
|
* @returns {Function} - A function that always passes null to it's callback as
|
||
|
* the error. The second argument to the callback will be an `object` with
|
||
|
* either an `error` or a `value` property.
|
||
|
* @example
|
||
|
*
|
||
|
* async.parallel([
|
||
|
* async.reflect(function(callback) {
|
||
|
* // do some stuff ...
|
||
|
* callback(null, 'one');
|
||
|
* }),
|
||
|
* async.reflect(function(callback) {
|
||
|
* // do some more stuff but error ...
|
||
|
* callback('bad stuff happened');
|
||
|
* }),
|
||
|
* async.reflect(function(callback) {
|
||
|
* // do some more stuff ...
|
||
|
* callback(null, 'two');
|
||
|
* })
|
||
|
* ],
|
||
|
* // optional callback
|
||
|
* function(err, results) {
|
||
|
* // values
|
||
|
* // results[0].value = 'one'
|
||
|
* // results[1].error = 'bad stuff happened'
|
||
|
* // results[2].value = 'two'
|
||
|
* });
|
||
|
*/
|
||
|
function reflect(fn) {
|
||
|
var _fn = (0, _wrapAsync2.default)(fn);
|
||
|
return (0, _initialParams2.default)(function reflectOn(args, reflectCallback) {
|
||
|
args.push(function callback(error, cbArg) {
|
||
|
if (error) {
|
||
|
reflectCallback(null, { error: error });
|
||
|
} else {
|
||
|
var value;
|
||
|
if (arguments.length <= 2) {
|
||
|
value = cbArg;
|
||
|
} else {
|
||
|
value = (0, _slice2.default)(arguments, 1);
|
||
|
}
|
||
|
reflectCallback(null, { value: value });
|
||
|
}
|
||
|
});
|
||
|
|
||
|
return _fn.apply(this, args);
|
||
|
});
|
||
|
}
|
||
|
module.exports = exports['default'];
|