* 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)
| 16623 | * @returns {object} Promise manager. |
| 16624 | */ |
| 16625 | function qFactory(nextTick, exceptionHandler) { |
| 16626 | var $qMinErr = minErr('$q', TypeError); |
| 16627 | |
| 16628 | /** |
| 16629 | * @ngdoc method |
| 16630 | * @name ng.$q#defer |
| 16631 | * @kind function |
| 16632 | * |
| 16633 | * @description |
| 16634 | * Creates a `Deferred` object which represents a task which will finish in the future. |
| 16635 | * |
| 16636 | * @returns {Deferred} Returns a new instance of deferred. |
| 16637 | */ |
| 16638 | function defer() { |
| 16639 | var d = new Deferred(); |
| 16640 | //Necessary to support unbound execution :/ |
| 16641 | d.resolve = simpleBind(d, d.resolve); |
| 16642 | d.reject = simpleBind(d, d.reject); |
| 16643 | d.notify = simpleBind(d, d.notify); |
| 16644 | return d; |
| 16645 | } |
| 16646 | |
| 16647 | function Promise() { |
| 16648 | this.$$state = { status: 0 }; |
| 16649 | } |
| 16650 | |
| 16651 | extend(Promise.prototype, { |
| 16652 | then: function(onFulfilled, onRejected, progressBack) { |
| 16653 | if (isUndefined(onFulfilled) && isUndefined(onRejected) && isUndefined(progressBack)) { |
| 16654 | return this; |
| 16655 | } |
| 16656 | var result = new Deferred(); |
| 16657 | |
| 16658 | this.$$state.pending = this.$$state.pending || []; |
| 16659 | this.$$state.pending.push([result, onFulfilled, onRejected, progressBack]); |
| 16660 | if (this.$$state.status > 0) scheduleProcessQueue(this.$$state); |
| 16661 | |
| 16662 | return result.promise; |
| 16663 | }, |
| 16664 | |
| 16665 | 'catch': function(callback) { |
| 16666 | return this.then(null, callback); |
| 16667 | }, |
| 16668 | |
| 16669 | 'finally': function(callback, progressBack) { |
| 16670 | return this.then(function(value) { |
| 16671 | return handleCallback(value, resolve, callback); |
| 16672 | }, function(error) { |
| 16673 | return handleCallback(error, reject, callback); |
| 16674 | }, progressBack); |
| 16675 | } |
| 16676 | }); |
| 16677 | |
| 16678 | //Faster, more basic than angular.bind http://jsperf.com/angular-bind-vs-custom-vs-native |
| 16679 | function simpleBind(context, fn) { |
| 16680 | return function(value) { |
| 16681 | fn.call(context, value); |
| 16682 | }; |
no test coverage detected