* 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)
| 17300 | * @returns {object} Promise manager. |
| 17301 | */ |
| 17302 | function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) { |
| 17303 | var $qMinErr = minErr('$q', TypeError); |
| 17304 | var queueSize = 0; |
| 17305 | var checkQueue = []; |
| 17306 | |
| 17307 | /** |
| 17308 | * @ngdoc method |
| 17309 | * @name ng.$q#defer |
| 17310 | * @kind function |
| 17311 | * |
| 17312 | * @description |
| 17313 | * Creates a `Deferred` object which represents a task which will finish in the future. |
| 17314 | * |
| 17315 | * @returns {Deferred} Returns a new instance of deferred. |
| 17316 | */ |
| 17317 | function defer() { |
| 17318 | return new Deferred(); |
| 17319 | } |
| 17320 | |
| 17321 | function Deferred() { |
| 17322 | var promise = this.promise = new Promise(); |
| 17323 | //Non prototype methods necessary to support unbound execution :/ |
| 17324 | this.resolve = function(val) { resolvePromise(promise, val); }; |
| 17325 | this.reject = function(reason) { rejectPromise(promise, reason); }; |
| 17326 | this.notify = function(progress) { notifyPromise(promise, progress); }; |
| 17327 | } |
| 17328 | |
| 17329 | |
| 17330 | function Promise() { |
| 17331 | this.$$state = { status: 0 }; |
| 17332 | } |
| 17333 | |
| 17334 | extend(Promise.prototype, { |
| 17335 | then: function(onFulfilled, onRejected, progressBack) { |
| 17336 | if (isUndefined(onFulfilled) && isUndefined(onRejected) && isUndefined(progressBack)) { |
| 17337 | return this; |
| 17338 | } |
| 17339 | var result = new Promise(); |
| 17340 | |
| 17341 | this.$$state.pending = this.$$state.pending || []; |
| 17342 | this.$$state.pending.push([result, onFulfilled, onRejected, progressBack]); |
| 17343 | if (this.$$state.status > 0) scheduleProcessQueue(this.$$state); |
| 17344 | |
| 17345 | return result; |
| 17346 | }, |
| 17347 | |
| 17348 | 'catch': function(callback) { |
| 17349 | return this.then(null, callback); |
| 17350 | }, |
| 17351 | |
| 17352 | 'finally': function(callback, progressBack) { |
| 17353 | return this.then(function(value) { |
| 17354 | return handleCallback(value, resolve, callback); |
| 17355 | }, function(error) { |
| 17356 | return handleCallback(error, reject, callback); |
| 17357 | }, progressBack); |
| 17358 | } |
| 17359 | }); |
no test coverage detected