|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- 'use strict';
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = asyncify;
-
- var _isObject = require('lodash/isObject');
-
- var _isObject2 = _interopRequireDefault(_isObject);
-
- var _initialParams = require('./internal/initialParams');
-
- var _initialParams2 = _interopRequireDefault(_initialParams);
-
- var _setImmediate = require('./internal/setImmediate');
-
- var _setImmediate2 = _interopRequireDefault(_setImmediate);
-
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
-
-
- function asyncify(func) {
- return (0, _initialParams2.default)(function (args, callback) {
- var result;
- try {
- result = func.apply(this, args);
- } catch (e) {
- return callback(e);
- }
-
- if ((0, _isObject2.default)(result) && typeof result.then === 'function') {
- result.then(function (value) {
- invokeCallback(callback, null, value);
- }, function (err) {
- invokeCallback(callback, err.message ? err : new Error(err));
- });
- } else {
- callback(null, result);
- }
- });
- }
-
- function invokeCallback(callback, error, value) {
- try {
- callback(error, value);
- } catch (e) {
- (0, _setImmediate2.default)(rethrow, e);
- }
- }
-
- function rethrow(error) {
- throw error;
- }
- module.exports = exports['default'];
|