* 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. * @returns {object} Promise manager.
(nextTick, exceptionHandler)
| 15479 | * @returns {object} Promise manager. |
| 15480 | */ |
| 15481 | function qFactory(nextTick, exceptionHandler) { |
| 15482 | var $qMinErr = minErr('$q', TypeError); |
| 15483 | |
| 15484 | /** |
| 15485 | * @ngdoc method |
| 15486 | * @name ng.$q#defer |
| 15487 | * @kind function |
| 15488 | * |
| 15489 | * @description |
| 15490 | * Creates a `Deferred` object which represents a task which will finish in the future. |
| 15491 | * |
| 15492 | * @returns {Deferred} Returns a new instance of deferred. |
| 15493 | */ |
| 15494 | var defer = function() { |
| 15495 | var d = new Deferred(); |
| 15496 | //Necessary to support unbound execution :/ |
| 15497 | d.resolve = simpleBind(d, d.resolve); |
| 15498 | d.reject = simpleBind(d, d.reject); |
| 15499 | d.notify = simpleBind(d, d.notify); |
| 15500 | return d; |
| 15501 | }; |
| 15502 | |
| 15503 | function Promise() { |
| 15504 | this.$$state = { status: 0 }; |
| 15505 | } |
| 15506 | |
| 15507 | extend(Promise.prototype, { |
| 15508 | then: function(onFulfilled, onRejected, progressBack) { |
| 15509 | if (isUndefined(onFulfilled) && isUndefined(onRejected) && isUndefined(progressBack)) { |
| 15510 | return this; |
| 15511 | } |
| 15512 | var result = new Deferred(); |
| 15513 | |
| 15514 | this.$$state.pending = this.$$state.pending || []; |
| 15515 | this.$$state.pending.push([result, onFulfilled, onRejected, progressBack]); |
| 15516 | if (this.$$state.status > 0) scheduleProcessQueue(this.$$state); |
| 15517 | |
| 15518 | return result.promise; |
| 15519 | }, |
| 15520 | |
| 15521 | "catch": function(callback) { |
| 15522 | return this.then(null, callback); |
| 15523 | }, |
| 15524 | |
| 15525 | "finally": function(callback, progressBack) { |
| 15526 | return this.then(function(value) { |
| 15527 | return handleCallback(value, true, callback); |
| 15528 | }, function(error) { |
| 15529 | return handleCallback(error, false, callback); |
| 15530 | }, progressBack); |
| 15531 | } |
| 15532 | }); |
| 15533 | |
| 15534 | //Faster, more basic than angular.bind http://jsperf.com/angular-bind-vs-custom-vs-native |
| 15535 | function simpleBind(context, fn) { |
| 15536 | return function(value) { |
| 15537 | fn.call(context, value); |
| 15538 | }; |
no test coverage detected