'use strict';12Object.defineProperty(exports, "__esModule", {3value: true4});5exports.default = asyncify;67var _initialParams = require('./internal/initialParams.js');89var _initialParams2 = _interopRequireDefault(_initialParams);1011var _setImmediate = require('./internal/setImmediate.js');1213var _setImmediate2 = _interopRequireDefault(_setImmediate);1415var _wrapAsync = require('./internal/wrapAsync.js');1617function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }1819/**20* Take a sync function and make it async, passing its return value to a21* callback. This is useful for plugging sync functions into a waterfall,22* series, or other async functions. Any arguments passed to the generated23* function will be passed to the wrapped function (except for the final24* callback argument). Errors thrown will be passed to the callback.25*26* If the function passed to `asyncify` returns a Promise, that promises's27* resolved/rejected state will be used to call the callback, rather than simply28* the synchronous return value.29*30* This also means you can asyncify ES2017 `async` functions.31*32* @name asyncify33* @static34* @memberOf module:Utils35* @method36* @alias wrapSync37* @category Util38* @param {Function} func - The synchronous function, or Promise-returning39* function to convert to an {@link AsyncFunction}.40* @returns {AsyncFunction} An asynchronous wrapper of the `func`. To be41* invoked with `(args..., callback)`.42* @example43*44* // passing a regular synchronous function45* async.waterfall([46* async.apply(fs.readFile, filename, "utf8"),47* async.asyncify(JSON.parse),48* function (data, next) {49* // data is the result of parsing the text.50* // If there was a parsing error, it would have been caught.51* }52* ], callback);53*54* // passing a function returning a promise55* async.waterfall([56* async.apply(fs.readFile, filename, "utf8"),57* async.asyncify(function (contents) {58* return db.model.create(contents);59* }),60* function (model, next) {61* // `model` is the instantiated model object.62* // If there was an error, this function would be skipped.63* }64* ], callback);65*66* // es2017 example, though `asyncify` is not needed if your JS environment67* // supports async functions out of the box68* var q = async.queue(async.asyncify(async function(file) {69* var intermediateStep = await processFile(file);70* return await somePromise(intermediateStep)71* }));72*73* q.push(files);74*/75function asyncify(func) {76if ((0, _wrapAsync.isAsync)(func)) {77return function (...args /*, callback*/) {78const callback = args.pop();79const promise = func.apply(this, args);80return handlePromise(promise, callback);81};82}8384return (0, _initialParams2.default)(function (args, callback) {85var result;86try {87result = func.apply(this, args);88} catch (e) {89return callback(e);90}91// if result is Promise object92if (result && typeof result.then === 'function') {93return handlePromise(result, callback);94} else {95callback(null, result);96}97});98}99100function handlePromise(promise, callback) {101return promise.then(value => {102invokeCallback(callback, null, value);103}, err => {104invokeCallback(callback, err && err.message ? err : new Error(err));105});106}107108function invokeCallback(callback, error, value) {109try {110callback(error, value);111} catch (err) {112(0, _setImmediate2.default)(e => {113throw e;114}, err);115}116}117module.exports = exports['default'];118119