Questions tagged [angular-promise]

Angular's $q promises provide a powerful abstraction over flow control. If you tag your question with this tag, consider also tagging it with "promise" tag. This tag is also appropriate for questions about angular and promises not relaying to $q directly.

Angular's $q promises provide a powerful abstraction over flow control. If you tag your question with this tag consider also tagging it with the promise tag.

This tag is also appropriate for questions about angular and promises not relaying to $q directly.

2114 questions
1848
votes
34 answers

What is the difference between Promises and Observables?

What is 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?
Rohit
  • 18,575
  • 3
  • 11
  • 9
486
votes
10 answers

AngularJS : Initialize service with asynchronous data

I have an AngularJS service that I want to initialize with some asynchronous data. Something like this: myModule.service('MyService', function($http) { var myData = null; $http.get('data.json').success(function (data) { myData =…
testing123
  • 11,367
  • 10
  • 47
  • 61
269
votes
14 answers

How can I access the value of a promise?

I'm looking at this example from Angular's documentation for $q, but I think this probably applies to promises in general. The example below is copied verbatim from their documentation with their comment included: promiseB =…
temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
196
votes
8 answers

How to cancel an $http request in AngularJS?

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).
mpm
  • 20,148
  • 7
  • 50
  • 55
109
votes
1 answer

Does never resolved promise cause memory leak?

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',…
Umut Benzer
  • 3,476
  • 4
  • 36
  • 53
108
votes
6 answers

Wait for all promises to resolve

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)…
jensengar
  • 6,117
  • 17
  • 58
  • 90
75
votes
11 answers

Angular 1.6.0: "Possibly unhandled rejection" error

We have a pattern for resolving promises in our Angular app that has served us well up until Angular 1.6.0: resource.get().$promise .then(function (response) { // do something with the response }, function (error) { …
Groucho
  • 1,055
  • 1
  • 7
  • 11
74
votes
4 answers

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

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…
SebastianRiemer
  • 1,495
  • 2
  • 20
  • 33
67
votes
6 answers

Error handling in AngularJS http get then construct

How can I handle an HTTP error, e.g. 500, when using the AngularJS "http get then" construct (promises)? $http.get(url).then( function(response) { console.log('get',response) } ) Problem is, for any non 200 HTTP response, the inner…
Federico Elles
  • 4,659
  • 7
  • 27
  • 35
66
votes
7 answers

What happens with $q.all() when some calls work and others fail?

What happens with $q.all() when some calls work and others fail? I have the following code: var entityIdColumn = $scope.entityType.toLowerCase() + 'Id'; var requests = $scope.grid.data .filter(function (rowData, i) { return…
Alan2
  • 23,493
  • 79
  • 256
  • 450
57
votes
1 answer

javascript, promises, how to access variable this inside a then scope

I want to be able to call a function inside the .then scope, and for that I use the this.foo() manner. But if I do this inside the .then I get an error, since this appears to be lost. What can I do? In this code, this would be equivalent to have the…
GWorking
  • 4,011
  • 10
  • 49
  • 90
50
votes
7 answers

How to return a resolved promise from an AngularJS Service using $q?

My service is: myApp.service('userService', [ '$http', '$q', '$rootScope', '$location', function($http, $q, $rootScope, $location) { var deferred; deferred = $q.defer(); this.initialized = deferred.promise; this.user = { …
user3620820
  • 513
  • 1
  • 4
  • 4
45
votes
2 answers

How do I use Bluebird with Angular?

I tried using Angular with Bluebird promises: HTML:
{{name}} {{also}}
JS: // javascript var app = angular.module('HelloApp', []); app.controller("HomeController",…
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
43
votes
2 answers

How to handle error in angular-ui-router's resolve

I am using angular-ui-router's resolve to get data from server before moving to a state. Sometimes the request to the server fails and I need to inform the user about the failure. If I call the server from the controller, I can put then and call my…
Endy Tjahjono
  • 24,120
  • 23
  • 83
  • 123
41
votes
4 answers

Get state of Angular deferred?

With jQuery deferreds I'm used to be able to check the current state like this: var defer = $.Deferred(); defer.state(); //Returns the state of the deferred, eg 'resolved' Is there a way to do the same for Angular deferreds? (or even better…
Evan Hobbs
  • 3,552
  • 5
  • 30
  • 42
1
2 3
99 100