-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathpromisify.js
More file actions
67 lines (56 loc) · 1.79 KB
/
promisify.js
File metadata and controls
67 lines (56 loc) · 1.79 KB
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
+(function (factory) {
if (typeof exports === 'undefined') {
factory(webduino || {});
} else {
module.exports = factory;
}
}(function (scope) {
'use strict';
if (typeof exports !== 'undefined' && typeof Promise === 'undefined') {
Promise = require('es6-promise').Promise;
}
// source:
// https://raw.githubusercontent.com/twistdigital/es6-promisify/release/2.0.0/lib/promisify.js
// Promise Context object constructor.
function Context(resolve, reject, custom) {
this.resolve = resolve;
this.reject = reject;
this.custom = custom;
}
// Default callback function - rejects on truthy error, otherwise resolves
function callback(ctx, err, result) {
if (typeof ctx.custom === 'function') {
var cust = function () {
// Bind the callback to itself, so the resolve and reject
// properties that we bound are available to the callback.
// Then we push it onto the end of the arguments array.
return ctx.custom.apply(cust, arguments);
};
cust.resolve = ctx.resolve;
cust.reject = ctx.reject;
cust.call(null, err, result);
} else {
if (err) {
return ctx.reject(err);
}
ctx.resolve(result);
}
}
function promisify(original, custom) {
return function () {
// Store original context
var that = this,
args = Array.prototype.slice.call(arguments);
// Return the promisified function
return new Promise(function (resolve, reject) {
// Create a Context object
var ctx = new Context(resolve, reject, custom);
// Append the callback bound to the context
args.push(callback.bind(null, ctx));
// Call the function
original.apply(that, args);
});
};
}
scope.util.promisify = promisify;
}));