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;
8 function spreadAdapter(val, nodeback) {
10 if (!util.isArray(val)) return successAdapter.call(promise, val, nodeback);
12 tryCatch(nodeback).apply(promise._boundValue(), [null].concat(val));
13 if (ret === errorObj) {
14 async.throwLater(ret.e);
18 function successAdapter(val, nodeback) {
20 var receiver = promise._boundValue();
21 var ret = val === undefined
22 ? tryCatch(nodeback).call(receiver, null)
23 : tryCatch(nodeback).call(receiver, null, val);
24 if (ret === errorObj) {
25 async.throwLater(ret.e);
28 function errorAdapter(reason, nodeback) {
31 var target = promise._target();
32 var newReason = target._getCarriedStackTrace();
33 newReason.cause = reason;
36 var ret = tryCatch(nodeback).call(promise._boundValue(), reason);
37 if (ret === errorObj) {
38 async.throwLater(ret.e);
42 Promise.prototype.asCallback =
43 Promise.prototype.nodeify = function (nodeback, options) {
44 if (typeof nodeback == "function") {
45 var adapter = successAdapter;
46 if (options !== undefined && Object(options).spread) {
47 adapter = spreadAdapter;