source: trip-planner-front/node_modules/async/ensureAsync.js@ 76712b2

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

initial commit

  • Property mode set to 100644
File size: 2.5 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = ensureAsync;
7
8var _setImmediate = require('./internal/setImmediate');
9
10var _setImmediate2 = _interopRequireDefault(_setImmediate);
11
12var _initialParams = require('./internal/initialParams');
13
14var _initialParams2 = _interopRequireDefault(_initialParams);
15
16var _wrapAsync = require('./internal/wrapAsync');
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20/**
21 * Wrap an async function and ensure it calls its callback on a later tick of
22 * the event loop. If the function already calls its callback on a next tick,
23 * no extra deferral is added. This is useful for preventing stack overflows
24 * (`RangeError: Maximum call stack size exceeded`) and generally keeping
25 * [Zalgo](http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony)
26 * contained. ES2017 `async` functions are returned as-is -- they are immune
27 * to Zalgo's corrupting influences, as they always resolve on a later tick.
28 *
29 * @name ensureAsync
30 * @static
31 * @memberOf module:Utils
32 * @method
33 * @category Util
34 * @param {AsyncFunction} fn - an async function, one that expects a node-style
35 * callback as its last argument.
36 * @returns {AsyncFunction} Returns a wrapped function with the exact same call
37 * signature as the function passed in.
38 * @example
39 *
40 * function sometimesAsync(arg, callback) {
41 * if (cache[arg]) {
42 * return callback(null, cache[arg]); // this would be synchronous!!
43 * } else {
44 * doSomeIO(arg, callback); // this IO would be asynchronous
45 * }
46 * }
47 *
48 * // this has a risk of stack overflows if many results are cached in a row
49 * async.mapSeries(args, sometimesAsync, done);
50 *
51 * // this will defer sometimesAsync's callback if necessary,
52 * // preventing stack overflows
53 * async.mapSeries(args, async.ensureAsync(sometimesAsync), done);
54 */
55function ensureAsync(fn) {
56 if ((0, _wrapAsync.isAsync)(fn)) return fn;
57 return (0, _initialParams2.default)(function (args, callback) {
58 var sync = true;
59 args.push(function () {
60 var innerArgs = arguments;
61 if (sync) {
62 (0, _setImmediate2.default)(function () {
63 callback.apply(null, innerArgs);
64 });
65 } else {
66 callback.apply(null, innerArgs);
67 }
68 });
69 fn.apply(this, args);
70 sync = false;
71 });
72}
73module.exports = exports['default'];
Note: See TracBrowser for help on using the repository browser.