* 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} Promi
(nextTick, exceptionHandler)
| 15938 | * @returns {object} Promise manager. |
| 15939 | */ |
| 15940 | function qFactory(nextTick, exceptionHandler) { |
| 15941 | var $qMinErr = minErr('$q', TypeError); |
| 15942 | |
| 15943 | /** |
| 15944 | * @ngdoc method |
| 15945 | * @name ng.$q#defer |
| 15946 | * @kind function |
| 15947 | * |
| 15948 | * @description |
| 15949 | * Creates a `Deferred` object which represents a task which will finish in the future. |
| 15950 | * |
| 15951 | * @returns {Deferred} Returns a new instance of deferred. |
| 15952 | */ |
| 15953 | var defer = function () { |
| 15954 | var d = new Deferred(); |
| 15955 | //Necessary to support unbound execution :/ |
| 15956 | d.resolve = simpleBind(d, d.resolve); |
| 15957 | d.reject = simpleBind(d, d.reject); |
| 15958 | d.notify = simpleBind(d, d.notify); |
| 15959 | return d; |
| 15960 | }; |
| 15961 | |
| 15962 | function Promise() { |
| 15963 | this.$$state = { status: 0 }; |
| 15964 | } |
| 15965 | |
| 15966 | extend(Promise.prototype, |
| 15967 | { |
| 15968 | then: function (onFulfilled, onRejected, progressBack) { |
| 15969 | if (isUndefined(onFulfilled) && isUndefined(onRejected) && isUndefined(progressBack)) { |
| 15970 | return this; |
| 15971 | } |
| 15972 | var result = new Deferred(); |
| 15973 | |
| 15974 | this.$$state.pending = this.$$state.pending || []; |
| 15975 | this.$$state.pending.push([result, onFulfilled, onRejected, progressBack]); |
| 15976 | if (this.$$state.status > 0) scheduleProcessQueue(this.$$state); |
| 15977 | |
| 15978 | return result.promise; |
| 15979 | }, |
| 15980 | |
| 15981 | "catch": function (callback) { |
| 15982 | return this.then(null, callback); |
| 15983 | }, |
| 15984 | |
| 15985 | "finally": function (callback, progressBack) { |
| 15986 | return this.then(function (value) { |
| 15987 | return handleCallback(value, true, callback); |
| 15988 | }, |
| 15989 | function (error) { |
| 15990 | return handleCallback(error, false, callback); |
| 15991 | }, |
| 15992 | progressBack); |
| 15993 | } |
| 15994 | }); |
| 15995 | |
| 15996 | //Faster, more basic than angular.bind http://jsperf.com/angular-bind-vs-custom-vs-native |
| 15997 | function simpleBind(context, fn) { |
no test coverage detected