angular-promise

AngularJS : $q -> deferred API order of things (lifecycle) AND who invokes digest?

大兔子大兔子 提交于 2019-11-26 07:39:49
问题 The $q service is very powerful in angularjs and make our life easier with asynchronous code. I am new to angular but using deferred API is not very new to me. I must say that I completely ok with the How to use part of documentation + there are very useful links for that within the docs + I checked out the source either. My question is more about the under the hood parts of deferred and promise API objects in angular. What are the exact phases in their life cycles and how are they interacts

angular $q, How to chain multiple promises within and after a for-loop

假装没事ソ 提交于 2019-11-26 06:20:00
问题 I want to have a for-loop which calls async functions each iteration. After the for-loop I want to execute another code block, but not before all the previous calls in the for-loop have been resolved. My problem at the moment is, that either the code-block after the for-loop is executed before all async calls have finished OR it is not executed at all. The code part with the FOR-loop and the code block after it (for complete code, please see fiddle): [..] function outerFunction($q, $scope) {

Does never resolved promise cause memory leak?

时光毁灭记忆、已成空白 提交于 2019-11-26 04:11:50
问题 I have a Promise . I created it to cancel an AJAX request if needed. But since I don\'t need to cancel that AJAX, I\'ve never resolved it and AJAX completed successfully. A simplified snippet: var defer = $q.defer(); $http({url: \'example.com/some/api\', timeout: defer.promise}).success(function(data) { // do something }); // Never defer.resolve() because I don\'t need to cancel that ajax. What happens to this promise after request? Do never resolved promises like that cause memory leaks? Do

How to use $http promise response outside success handler

大兔子大兔子 提交于 2019-11-26 03:28:46
问题 $scope.tempObject = {}; $http({ method: \'GET\', url: \'/myRestUrl\' }).then(function successCallback(response) { $scope.tempObject = response console.log(\"Temp Object in successCallback \", $scope.tempObject); }, function errorCallback(response) { }); console.log(\"Temp Object outside $http \", $scope.tempObject); I am getting response in successCallback but not getting $scope.tempObject outside $http . its showing undefined . How to access response or $scope.tempObject after $http 回答1: But

Wait for all promises to resolve

匆匆过客 提交于 2019-11-26 03:08:20
问题 So I have a situation where I have multiple promise chains of an unknown length. I want some action to run when all the CHAINS have been processed. Is that even possible? Here is an example: app.controller(\'MainCtrl\', function($scope, $q, $timeout) { var one = $q.defer(); var two = $q.defer(); var three = $q.defer(); var all = $q.all([one.promise, two.promise, three.promise]); all.then(allSuccess); function success(data) { console.log(data); return data + \"Chained\"; } function allSuccess(

Caching a promise object in AngularJS service

你离开我真会死。 提交于 2019-11-26 01:44:02
问题 I want to implement a dynamic loading of a static resource in AngularJS using Promises. The problem: I have couple components on page which might (or not, depends which are displayed, thus dynamic) need to get a static resource from the server. Once loaded, it can be cached for the whole application life. I have implemented this mechanism, but I\'m new to Angular and Promises, and I want to make sure if this is a right solution \\ approach. var data = null; var deferredLoadData = null;

How to cancel an $http request in AngularJS?

◇◆丶佛笑我妖孽 提交于 2019-11-26 00:13:22
问题 Given a Ajax request in AngularJS $http.get(\"/backend/\").success(callback); what is the most effective way to cancel that request if another request is launched (same backend, different parameters for instance). 回答1: This feature was added to the 1.1.5 release via a timeout parameter: var canceler = $q.defer(); $http.get('/someUrl', {timeout: canceler.promise}).success(successCallback); // later... canceler.resolve(); // Aborts the $http request if it isn't finished. 回答2: Cancelling Angular

AngularJS execution order with `$q` — Chaining Promises

徘徊边缘 提交于 2019-11-25 23:35:42
问题 Following Approach works: $q.when() .then(checkCookieToken) // check if cookie already exists e.g. in cookie .then(setHeader) // set Header with REST-Token e.g from cookie .then(checkTokenOnline) // if not OK logout .then(getMenu) // if previous OK get navigation menu .then(getDataResource) // set ngResource .then(getData); // and query it 4 Questions: 1) If e.g. checkTokenOnline is not OK, I don\'t want to execute the rest functions, how can I quit (exit, break, whatever,..) at this point? 2

Why are Callbacks from Promise `.then` Methods an Anti-Pattern

冷暖自知 提交于 2019-11-25 23:17:07
问题 I have seen answers on StackOverflow where people suggest furnishing a callback function to an AngularJS service. app.controller(\'tokenCtrl\', function($scope, tokenService) { tokenService.getTokens(function callbackFn(tokens) { $scope.tokens = tokens; }); }); app.factory(\'tokenService\', function($http) { var getTokens = function(callbackFn) { $http.get(\'/api/tokens\').then (function onFulfilled(response) { callbackFn(response.data); }); }; return { getTokens: getTokens }; }); This seems

What is the difference between Promises and Observables?

强颜欢笑 提交于 2019-11-25 22:26:16
问题 Can someone please explain the difference between Promise and Observable in Angular? An example on each would be helpful in understanding both the cases. In what scenario can we use each case? 回答1: Promise A Promise handles a single event when an async operation completes or fails. Note: There are Promise libraries out there that support cancellation, but ES6 Promise doesn't so far. Observable An Observable is like a Stream (in many languages) and allows to pass zero or more events where the