* Constructs a promise manager. * * @param {function(function)} nextTick Function for executing functions in the next turn. * @param {function(...*)} exceptionHandler Function into which unexpected exceptions are passed for * debugging purposes. * @param {boolean=} errorOnUnhandledRejection
(nextTick, exceptionHandler, errorOnUnhandledRejections)
| 17256 | * @returns {object} Promise manager. |
| 17257 | */ |
| 17258 | function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) { |
| 17259 | var $qMinErr = minErr('$q', TypeError); |
| 17260 | var queueSize = 0; |
| 17261 | var checkQueue = []; |
| 17262 | |
| 17263 | /** |
| 17264 | * @ngdoc method |
| 17265 | * @name ng.$q#defer |
| 17266 | * @kind function |
| 17267 | * |
| 17268 | * @description |
| 17269 | * Creates a `Deferred` object which represents a task which will finish in the future. |
| 17270 | * |
| 17271 | * @returns {Deferred} Returns a new instance of deferred. |
| 17272 | */ |
| 17273 | function defer() { |
| 17274 | return new Deferred(); |
| 17275 | } |
| 17276 | |
| 17277 | function Deferred() { |
| 17278 | var promise = this.promise = new Promise(); |
| 17279 | //Non prototype methods necessary to support unbound execution :/ |
| 17280 | this.resolve = function(val) { resolvePromise(promise, val); }; |
| 17281 | this.reject = function(reason) { rejectPromise(promise, reason); }; |
| 17282 | this.notify = function(progress) { notifyPromise(promise, progress); }; |
| 17283 | } |
| 17284 | |
| 17285 | |
| 17286 | function Promise() { |
| 17287 | this.$$state = { status: 0 }; |
| 17288 | } |
| 17289 | |
| 17290 | extend(Promise.prototype, { |
| 17291 | then: function(onFulfilled, onRejected, progressBack) { |
| 17292 | if (isUndefined(onFulfilled) && isUndefined(onRejected) && isUndefined(progressBack)) { |
| 17293 | return this; |
| 17294 | } |
| 17295 | var result = new Promise(); |
| 17296 | |
| 17297 | this.$$state.pending = this.$$state.pending || []; |
| 17298 | this.$$state.pending.push([result, onFulfilled, onRejected, progressBack]); |
| 17299 | if (this.$$state.status > 0) scheduleProcessQueue(this.$$state); |
| 17300 | |
| 17301 | return result; |
| 17302 | }, |
| 17303 | |
| 17304 | 'catch': function(callback) { |
| 17305 | return this.then(null, callback); |
| 17306 | }, |
| 17307 | |
| 17308 | 'finally': function(callback, progressBack) { |
| 17309 | return this.then(function(value) { |
| 17310 | return handleCallback(value, resolve, callback); |
| 17311 | }, function(error) { |
| 17312 | return handleCallback(error, reject, callback); |
| 17313 | }, progressBack); |
| 17314 | } |
| 17315 | }); |
no test coverage detected