react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / har-validator / node_modules / bluebird / js / main / nodeify.js
81160 views"use strict";1module.exports = function(Promise) {2var util = require("./util.js");3var async = require("./async.js");4var tryCatch = util.tryCatch;5var errorObj = util.errorObj;67function spreadAdapter(val, nodeback) {8var promise = this;9if (!util.isArray(val)) return successAdapter.call(promise, val, nodeback);10var ret = tryCatch(nodeback).apply(promise._boundTo, [null].concat(val));11if (ret === errorObj) {12async.throwLater(ret.e);13}14}1516function successAdapter(val, nodeback) {17var promise = this;18var receiver = promise._boundTo;19var ret = val === undefined20? tryCatch(nodeback).call(receiver, null)21: tryCatch(nodeback).call(receiver, null, val);22if (ret === errorObj) {23async.throwLater(ret.e);24}25}26function errorAdapter(reason, nodeback) {27var promise = this;28if (!reason) {29var target = promise._target();30var newReason = target._getCarriedStackTrace();31newReason.cause = reason;32reason = newReason;33}34var ret = tryCatch(nodeback).call(promise._boundTo, reason);35if (ret === errorObj) {36async.throwLater(ret.e);37}38}3940Promise.prototype.asCallback =41Promise.prototype.nodeify = function (nodeback, options) {42if (typeof nodeback == "function") {43var adapter = successAdapter;44if (options !== undefined && Object(options).spread) {45adapter = spreadAdapter;46}47this._then(48adapter,49errorAdapter,50undefined,51this,52nodeback53);54}55return this;56};57};585960