|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"use strict"; |
|
var util = require("./util.js"); |
|
var maybeWrapAsError = util.maybeWrapAsError; |
|
var errors = require("./errors.js"); |
|
var TimeoutError = errors.TimeoutError; |
|
var RejectionError = errors.RejectionError; |
|
var async = require("./async.js"); |
|
var haveGetters = util.haveGetters; |
|
var es5 = require("./es5.js"); |
|
|
|
function isUntypedError(obj) { |
|
return obj instanceof Error && |
|
es5.getPrototypeOf(obj) === Error.prototype; |
|
} |
|
|
|
function wrapAsRejectionError(obj) { |
|
var ret; |
|
if (isUntypedError(obj)) { |
|
ret = new RejectionError(obj); |
|
} |
|
else { |
|
ret = obj; |
|
} |
|
errors.markAsOriginatingFromRejection(ret); |
|
return ret; |
|
} |
|
|
|
function nodebackForPromise(promise) { |
|
function PromiseResolver$_callback(err, value) { |
|
if (promise === null) return; |
|
|
|
if (err) { |
|
var wrapped = wrapAsRejectionError(maybeWrapAsError(err)); |
|
promise._attachExtraTrace(wrapped); |
|
promise._reject(wrapped); |
|
} |
|
else { |
|
if (arguments.length > 2) { |
|
var $_len = arguments.length;var args = new Array($_len - 1); for(var $_i = 1; $_i < $_len; ++$_i) {args[$_i - 1] = arguments[$_i];} |
|
promise._fulfill(args); |
|
} |
|
else { |
|
promise._fulfill(value); |
|
} |
|
} |
|
|
|
promise = null; |
|
} |
|
return PromiseResolver$_callback; |
|
} |
|
|
|
|
|
var PromiseResolver; |
|
if (!haveGetters) { |
|
PromiseResolver = function PromiseResolver(promise) { |
|
this.promise = promise; |
|
this.asCallback = nodebackForPromise(promise); |
|
this.callback = this.asCallback; |
|
}; |
|
} |
|
else { |
|
PromiseResolver = function PromiseResolver(promise) { |
|
this.promise = promise; |
|
}; |
|
} |
|
if (haveGetters) { |
|
var prop = { |
|
get: function() { |
|
return nodebackForPromise(this.promise); |
|
} |
|
}; |
|
es5.defineProperty(PromiseResolver.prototype, "asCallback", prop); |
|
es5.defineProperty(PromiseResolver.prototype, "callback", prop); |
|
} |
|
|
|
PromiseResolver._nodebackForPromise = nodebackForPromise; |
|
|
|
PromiseResolver.prototype.toString = function PromiseResolver$toString() { |
|
return "[object PromiseResolver]"; |
|
}; |
|
|
|
PromiseResolver.prototype.resolve = |
|
PromiseResolver.prototype.fulfill = function PromiseResolver$resolve(value) { |
|
var promise = this.promise; |
|
if (promise._tryFollow(value)) { |
|
return; |
|
} |
|
promise._fulfill(value); |
|
}; |
|
|
|
PromiseResolver.prototype.reject = function PromiseResolver$reject(reason) { |
|
var promise = this.promise; |
|
errors.markAsOriginatingFromRejection(reason); |
|
var trace = errors.canAttach(reason) ? reason : new Error(reason + ""); |
|
promise._attachExtraTrace(trace); |
|
promise._reject(reason); |
|
if (trace !== reason) { |
|
this._setCarriedStackTrace(trace); |
|
} |
|
}; |
|
|
|
PromiseResolver.prototype.progress = |
|
function PromiseResolver$progress(value) { |
|
this.promise._progress(value); |
|
}; |
|
|
|
PromiseResolver.prototype.cancel = function PromiseResolver$cancel() { |
|
this.promise.cancel((void 0)); |
|
}; |
|
|
|
PromiseResolver.prototype.timeout = function PromiseResolver$timeout() { |
|
this.reject(new TimeoutError("timeout")); |
|
}; |
|
|
|
PromiseResolver.prototype.isResolved = function PromiseResolver$isResolved() { |
|
return this.promise.isResolved(); |
|
}; |
|
|
|
PromiseResolver.prototype.toJSON = function PromiseResolver$toJSON() { |
|
return this.promise.toJSON(); |
|
}; |
|
|
|
PromiseResolver.prototype._setCarriedStackTrace = |
|
function PromiseResolver$_setCarriedStackTrace(trace) { |
|
if (this.promise.isRejected()) { |
|
this.promise._setCarriedStackTrace(trace); |
|
} |
|
}; |
|
|
|
module.exports = PromiseResolver; |
|
|