massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-json / node_modules / es6-promisify / dist / promisify.js
1 "use strict";
2
3 /* global module, require */
4 module.exports = function () {
5
6     "use strict";
7
8     // Get a promise object. This may be native, or it may be polyfilled
9
10     var ES6Promise = require("./promise.js");
11
12     /**
13      * thatLooksLikeAPromiseToMe()
14      *
15      * Duck-types a promise.
16      *
17      * @param {object} o
18      * @return {bool} True if this resembles a promise
19      */
20     function thatLooksLikeAPromiseToMe(o) {
21         return o && typeof o.then === "function" && typeof o.catch === "function";
22     }
23
24     /**
25      * promisify()
26      *
27      * Transforms callback-based function -- func(arg1, arg2 .. argN, callback) -- into
28      * an ES6-compatible Promise. Promisify provides a default callback of the form (error, result)
29      * and rejects when `error` is truthy. You can also supply settings object as the second argument.
30      *
31      * @param {function} original - The function to promisify
32      * @param {object} settings - Settings object
33      * @param {object} settings.thisArg - A `this` context to use. If not set, assume `settings` _is_ `thisArg`
34      * @param {bool} settings.multiArgs - Should multiple arguments be returned as an array?
35      * @return {function} A promisified version of `original`
36      */
37     return function promisify(original, settings) {
38
39         return function () {
40             for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
41                 args[_key] = arguments[_key];
42             }
43
44             var returnMultipleArguments = settings && settings.multiArgs;
45
46             var target = void 0;
47             if (settings && settings.thisArg) {
48                 target = settings.thisArg;
49             } else if (settings) {
50                 target = settings;
51             }
52
53             // Return the promisified function
54             return new ES6Promise(function (resolve, reject) {
55
56                 // Append the callback bound to the context
57                 args.push(function callback(err) {
58
59                     if (err) {
60                         return reject(err);
61                     }
62
63                     for (var _len2 = arguments.length, values = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
64                         values[_key2 - 1] = arguments[_key2];
65                     }
66
67                     if (false === !!returnMultipleArguments) {
68                         return resolve(values[0]);
69                     }
70
71                     resolve(values);
72                 });
73
74                 // Call the function
75                 var response = original.apply(target, args);
76
77                 // If it looks like original already returns a promise,
78                 // then just resolve with that promise. Hopefully, the callback function we added will just be ignored.
79                 if (thatLooksLikeAPromiseToMe(response)) {
80                     resolve(response);
81                 }
82             });
83         };
84     };
85 }();