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