Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81160 views
1
"use strict";
2
module.exports = function(Promise) {
3
var util = require("./util.js");
4
var async = require("./async.js");
5
var tryCatch = util.tryCatch;
6
var errorObj = util.errorObj;
7
8
function spreadAdapter(val, nodeback) {
9
var promise = this;
10
if (!util.isArray(val)) return successAdapter.call(promise, val, nodeback);
11
var ret = tryCatch(nodeback).apply(promise._boundTo, [null].concat(val));
12
if (ret === errorObj) {
13
async.throwLater(ret.e);
14
}
15
}
16
17
function successAdapter(val, nodeback) {
18
var promise = this;
19
var receiver = promise._boundTo;
20
var ret = val === undefined
21
? tryCatch(nodeback).call(receiver, null)
22
: tryCatch(nodeback).call(receiver, null, val);
23
if (ret === errorObj) {
24
async.throwLater(ret.e);
25
}
26
}
27
function errorAdapter(reason, nodeback) {
28
var promise = this;
29
if (!reason) {
30
var target = promise._target();
31
var newReason = target._getCarriedStackTrace();
32
newReason.cause = reason;
33
reason = newReason;
34
}
35
var ret = tryCatch(nodeback).call(promise._boundTo, reason);
36
if (ret === errorObj) {
37
async.throwLater(ret.e);
38
}
39
}
40
41
Promise.prototype.asCallback =
42
Promise.prototype.nodeify = function (nodeback, options) {
43
if (typeof nodeback == "function") {
44
var adapter = successAdapter;
45
if (options !== undefined && Object(options).spread) {
46
adapter = spreadAdapter;
47
}
48
this._then(
49
adapter,
50
errorAdapter,
51
undefined,
52
this,
53
nodeback
54
);
55
}
56
return this;
57
};
58
};
59
60