프로젝트

일반

사용자정보

통계
| 개정판:

root / HServer / 00.Server / 00.Program / node_modules / async / asyncify.js

이력 | 보기 | 이력해설 | 다운로드 (2.91 KB)

1
'use strict';
2

    
3
Object.defineProperty(exports, "__esModule", {
4
    value: true
5
});
6
exports.default = asyncify;
7

    
8
var _isObject = require('lodash/isObject');
9

    
10
var _isObject2 = _interopRequireDefault(_isObject);
11

    
12
var _initialParams = require('./internal/initialParams');
13

    
14
var _initialParams2 = _interopRequireDefault(_initialParams);
15

    
16
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17

    
18
/**
19
 * Take a sync function and make it async, passing its return value to a
20
 * callback. This is useful for plugging sync functions into a waterfall,
21
 * series, or other async functions. Any arguments passed to the generated
22
 * function will be passed to the wrapped function (except for the final
23
 * callback argument). Errors thrown will be passed to the callback.
24
 *
25
 * If the function passed to `asyncify` returns a Promise, that promises's
26
 * resolved/rejected state will be used to call the callback, rather than simply
27
 * the synchronous return value.
28
 *
29
 * This also means you can asyncify ES2016 `async` functions.
30
 *
31
 * @name asyncify
32
 * @static
33
 * @memberOf module:Utils
34
 * @method
35
 * @alias wrapSync
36
 * @category Util
37
 * @param {Function} func - The synchronous function to convert to an
38
 * asynchronous function.
39
 * @returns {Function} An asynchronous wrapper of the `func`. To be invoked with
40
 * (callback).
41
 * @example
42
 *
43
 * // passing a regular synchronous function
44
 * async.waterfall([
45
 *     async.apply(fs.readFile, filename, "utf8"),
46
 *     async.asyncify(JSON.parse),
47
 *     function (data, next) {
48
 *         // data is the result of parsing the text.
49
 *         // If there was a parsing error, it would have been caught.
50
 *     }
51
 * ], callback);
52
 *
53
 * // passing a function returning a promise
54
 * async.waterfall([
55
 *     async.apply(fs.readFile, filename, "utf8"),
56
 *     async.asyncify(function (contents) {
57
 *         return db.model.create(contents);
58
 *     }),
59
 *     function (model, next) {
60
 *         // `model` is the instantiated model object.
61
 *         // If there was an error, this function would be skipped.
62
 *     }
63
 * ], callback);
64
 *
65
 * // es6 example
66
 * var q = async.queue(async.asyncify(async function(file) {
67
 *     var intermediateStep = await processFile(file);
68
 *     return await somePromise(intermediateStep)
69
 * }));
70
 *
71
 * q.push(files);
72
 */
73
function asyncify(func) {
74
    return (0, _initialParams2.default)(function (args, callback) {
75
        var result;
76
        try {
77
            result = func.apply(this, args);
78
        } catch (e) {
79
            return callback(e);
80
        }
81
        // if result is Promise object
82
        if ((0, _isObject2.default)(result) && typeof result.then === 'function') {
83
            result.then(function (value) {
84
                callback(null, value);
85
            }, function (err) {
86
                callback(err.message ? err : new Error(err));
87
            });
88
        } else {
89
            callback(null, result);
90
        }
91
    });
92
}
93
module.exports = exports['default'];