source: trip-planner-front/node_modules/async/asyncify.js@ 188ee53

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

initial commit

  • Property mode set to 100644
File size: 3.4 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = asyncify;
7
8var _isObject = require('lodash/isObject');
9
10var _isObject2 = _interopRequireDefault(_isObject);
11
12var _initialParams = require('./internal/initialParams');
13
14var _initialParams2 = _interopRequireDefault(_initialParams);
15
16var _setImmediate = require('./internal/setImmediate');
17
18var _setImmediate2 = _interopRequireDefault(_setImmediate);
19
20function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
22/**
23 * Take a sync function and make it async, passing its return value to a
24 * callback. This is useful for plugging sync functions into a waterfall,
25 * series, or other async functions. Any arguments passed to the generated
26 * function will be passed to the wrapped function (except for the final
27 * callback argument). Errors thrown will be passed to the callback.
28 *
29 * If the function passed to `asyncify` returns a Promise, that promises's
30 * resolved/rejected state will be used to call the callback, rather than simply
31 * the synchronous return value.
32 *
33 * This also means you can asyncify ES2017 `async` functions.
34 *
35 * @name asyncify
36 * @static
37 * @memberOf module:Utils
38 * @method
39 * @alias wrapSync
40 * @category Util
41 * @param {Function} func - The synchronous function, or Promise-returning
42 * function to convert to an {@link AsyncFunction}.
43 * @returns {AsyncFunction} An asynchronous wrapper of the `func`. To be
44 * invoked with `(args..., callback)`.
45 * @example
46 *
47 * // passing a regular synchronous function
48 * async.waterfall([
49 * async.apply(fs.readFile, filename, "utf8"),
50 * async.asyncify(JSON.parse),
51 * function (data, next) {
52 * // data is the result of parsing the text.
53 * // If there was a parsing error, it would have been caught.
54 * }
55 * ], callback);
56 *
57 * // passing a function returning a promise
58 * async.waterfall([
59 * async.apply(fs.readFile, filename, "utf8"),
60 * async.asyncify(function (contents) {
61 * return db.model.create(contents);
62 * }),
63 * function (model, next) {
64 * // `model` is the instantiated model object.
65 * // If there was an error, this function would be skipped.
66 * }
67 * ], callback);
68 *
69 * // es2017 example, though `asyncify` is not needed if your JS environment
70 * // supports async functions out of the box
71 * var q = async.queue(async.asyncify(async function(file) {
72 * var intermediateStep = await processFile(file);
73 * return await somePromise(intermediateStep)
74 * }));
75 *
76 * q.push(files);
77 */
78function asyncify(func) {
79 return (0, _initialParams2.default)(function (args, callback) {
80 var result;
81 try {
82 result = func.apply(this, args);
83 } catch (e) {
84 return callback(e);
85 }
86 // if result is Promise object
87 if ((0, _isObject2.default)(result) && typeof result.then === 'function') {
88 result.then(function (value) {
89 invokeCallback(callback, null, value);
90 }, function (err) {
91 invokeCallback(callback, err.message ? err : new Error(err));
92 });
93 } else {
94 callback(null, result);
95 }
96 });
97}
98
99function invokeCallback(callback, error, value) {
100 try {
101 callback(error, value);
102 } catch (e) {
103 (0, _setImmediate2.default)(rethrow, e);
104 }
105}
106
107function rethrow(error) {
108 throw error;
109}
110module.exports = exports['default'];
Note: See TracBrowser for help on using the repository browser.