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-promise
Filter by
Sorted by
Tagged with
1875 votes
35 answers
829k views

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's user avatar
  • 18.8k
487 votes
10 answers
222k views

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....
testing123's user avatar
  • 11.4k
279 votes
14 answers
718k views

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 ...
temporary_user_name's user avatar
196 votes
8 answers
131k views

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 ...
mpm's user avatar
  • 20.2k
113 votes
1 answer
30k views

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 ...
Umut Benzer's user avatar
  • 3,506
108 votes
6 answers
109k views

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....
jensengar's user avatar
  • 6,147
76 votes
11 answers
135k views

Angular 1.6.0: "Possibly unhandled rejection" error [duplicate]

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 ...
Groucho's user avatar
  • 1,065
74 votes
4 answers
82k views

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....
SebastianRiemer's user avatar
67 votes
6 answers
118k views

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) } ) ...
Federico Elles's user avatar
66 votes
7 answers
48k views

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 ...
Alan2's user avatar
  • 24k
58 votes
1 answer
78k views

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

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? ...
GWorking's user avatar
  • 4,181
50 votes
7 answers
121k views

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....
user3620820's user avatar
45 votes
2 answers
9k views

How do I use Bluebird with Angular?

I tried using Angular with Bluebird promises: HTML: <body ng-app="HelloApp"> <div ng-controller="HomeController">{{name}} {{also}}</div> </body> JS: // ...
Benjamin Gruenbaum's user avatar
43 votes
2 answers
30k views

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 ...
Endy Tjahjono's user avatar
41 votes
4 answers
44k views

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 ...
Evan Hobbs's user avatar
  • 3,592
35 votes
3 answers
13k views

Caching a promise object in AngularJS service

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) ...
andrew.fox's user avatar
  • 7,667
33 votes
2 answers
41k views

Angular2 Observable - Await multiple function calls before proceeding

I am trying to improve my knowledge of Angular2 by migrating an application currently written in Angular1/AngularJS. One feature in particular has me stumped. I am trying to replicate a feature where ...
Benjamin McFerren's user avatar
33 votes
9 answers
59k views

Possibly unhandled rejection in Angular 1.6

I have a code with AngularJS: service.doSomething() .then(function(result) { //do something with the result }); In AngularJS 1.5.9 when I have error in the .then() section like: service....
Piotr Pradzynski's user avatar
30 votes
3 answers
8k views

Angular $q .catch() method fails in IE8

I'm experiencing a weird bug on IE8 while trying to catch a promise reject (promise returned by a basic ngResource call) : This code work with .then(success, fail) syntax : promise.then(function(...
Jscti's user avatar
  • 14.2k
29 votes
5 answers
27k views

Immediately return a resolved promise using AngularJS

I'm trying to get my head around promises in JavaScript (in particular AngularJS). I have a function in a service, let's call it fooService, that checks if we've loaded some data. If it has, I just ...
samturner's user avatar
  • 2,223
25 votes
3 answers
12k views

$q promise error callback chains

In the following code snippet error 1 and success 2 will be logged. How can I can I propagate error callbacks being invoked rather than the success callbacks being invoked if the original deferred is ...
Steven Wexler's user avatar
25 votes
2 answers
20k views

What's the equivalent of Angular's $q in Angular2?

What's the equivalent of Angular's $q in Angular2? Specifically, I'm looking for $q.when, which allowed you to do something like: return $q.when(['TestResponse']);
David's user avatar
  • 15.9k
21 votes
1 answer
3k views

Intercept Unathorized API calls with Angular

I am trying to intercept the 401 and 403 errors to refresh the user token, but I can't get it working well. All I have achieved is this interceptor: app.config(function ($httpProvider) { $...
VanPersie's user avatar
  • 103
20 votes
2 answers
5k views

Promises - error callback vs. catch

Can somebody tell me if there is a difference between using an error callback vs. a catch function, when using $q.promise please? E.g. are the two snippets of code functionally equivalent? function ...
keldar's user avatar
  • 6,212
19 votes
3 answers
6k views

Typescript async/await and angular $q service

New TypeScript async/await feature uses ES6 promises. AngularJS uses $q service promises with slightly different interface. Is there any way to use TypeScript async/await feature with $q service ...
Random's user avatar
  • 3,917
18 votes
7 answers
65k views

Make angular.forEach wait for promise after going to next object

I have a list of objects. The objects are passed to a deferred function. I want to call the function with the next object only after the previous call is resolved. Is there any way I can do this? ...
Razvan's user avatar
  • 313
18 votes
3 answers
4k views

Is this a "Deferred Antipattern"?

I'm finding it hard to understand the "deferred antipattern". I think I understand it in principal but I haven't seen a super simple example of what a service, with a differed promise and one with ...
Aleski's user avatar
  • 1,432
18 votes
5 answers
16k views

AngularJS - Promises rethrow caught exceptions

In the following code, an exception is caught by the catch function of the $q promise: // Fiddle - http://jsfiddle.net/EFpn8/6/ f1().then(function(data) { console.log("success 1: "+data) ...
VitalyB's user avatar
  • 12.6k
18 votes
5 answers
16k views

Stop request in angularjs interceptor

How can I stop a request in Angularjs interceptor. Is there any way to do that? I tried using promises and sending reject instead of resolve ! .factory('connectionInterceptor', ['$q', '$timeout',...
vipulsodha's user avatar
18 votes
2 answers
44k views

How can I access a variable outside a promise `.then` method?

I'm working on a Spotify app. I'm able to login and get my token. My problem is I cannot access a variable outside the method. In this case "getCurrentUser" This is my method: function getUser() { ...
Rodolfo R's user avatar
  • 427
18 votes
1 answer
13k views

wait for promises in onbeforeunload

I want to send a $http.get if the page gets closed. Then I stumbold upon a problem where promises can't get resolved because if this one last method returns, the page gets destroyed. The following ...
noXi's user avatar
  • 183
17 votes
7 answers
31k views

How do I sequentially chain promises with angularjs $q?

In the promise library Q, you can do the following to sequentially chain promises: var items = ['one', 'two', 'three']; var chain = Q(); items.forEach(function (el) { chain = chain.then(foo(el)); })...
redgeoff's user avatar
  • 3,313
17 votes
3 answers
19k views

Chain Angular $http calls properly?

I have been reading about $q and promises for days now and I seem to understand it...somewhat. I have the following situation in practice: An $http request is made and checks whether a subsequent ...
VSO's user avatar
  • 12.1k
17 votes
3 answers
8k views

finally block in angular 2 promise

I observe that In angular 2 there is no finally block for promise API angular 1 : loadUsers() { fetch('/api/users').then((response) => { return response.json(); }).then((data) => { ...
Dmehro's user avatar
  • 1,279
16 votes
3 answers
23k views

Setting a timeout for each promise within a promise.all

I am able to successfully perform a Promise.all, and gracefully handle resolves and rejects. However, some promises complete within a few milliseconds, some can/could take a while. I want to be able ...
Oam Psy's user avatar
  • 8,613
16 votes
3 answers
15k views

Promise chaining when using $timeout

I'm trying to understand the promise API and chaining, particularly the timing when $timeoutis used with .then(). What I had expected from the following is that since $timeout returns a promise, .then(...
twip's user avatar
  • 648
14 votes
3 answers
3k views

Why do we prefer using $q in angular instead of $http [duplicate]

I am currently using $q service from angular to make API calls like this: var deferred = $q.defer(); $http.get(config.apiHost + details.url) .success(function (data) { deferred.resolve(...
Bhushan Goel's user avatar
  • 2,124
14 votes
3 answers
13k views

Swallowed message : Error: Uncaught (in promise): [object Undefined]

My login component briefly displays before being removed by an error message about an undefined object in a promise. Here is the promise definition: static init(): Promise<any> { ...
Stephane's user avatar
  • 12.3k
13 votes
5 answers
38k views

Subscription to promise

In my Angular 7 application I have next function: getUserData(uid) { return this.fireStore.collection('users').doc(uid).valueChanges().subscribe(data => { this.writeCookie(data) ...
Volodymyr Humeniuk's user avatar
13 votes
4 answers
92k views

Convert promise in JSON object

I have a problem converting the promise returned by the service to the controller. What I want is to create an array of JSON objects from the data contained in promise. Here is what I receive in the ...
VitezKoja's user avatar
  • 173
13 votes
3 answers
8k views

How to destroy unresolved promise

Have a look into the code snippet $scope.getSongs = function(keyword){ songServices.getSongList(keyword).then( function(resp){ $scope.songList = resp.data.songList; ...
Suraj Khurana's user avatar
13 votes
1 answer
7k views

angular $http / jquery complete equivalent

Is there a way to emulate jquery 'complete' callback with angular $http module? I have some code I would like to execute no matter whether the request succeeded or failed and at the moment I find ...
Renaud's user avatar
  • 4,638
12 votes
3 answers
11k views

Angular promise on multiple $http

I am trying to do multiple $http call and my code looks something like this: var data = ["data1","data2","data3"..."data10"]; for(var i=0;i<data.length;i++){ $http.get("http://example.com/"+...
user1995781's user avatar
  • 19.2k
12 votes
3 answers
12k views

What is the HTTP promise object in AngularJS?

Although I am working with the HTTP promise object in AngularJS, I don't have a clear concept of what an HTTP promise object actually is and what the is difference between an HTTP promise object and a ...
TanvirArjel's user avatar
  • 31.1k
12 votes
1 answer
12k views

Executing then after catch

I have the following fiddle: http://jsfiddle.net/thelgevold/3uv9nnjm/6/ angular.module('hello',[]).controller('helloController',function($q){ console.clear(); function someService(){ ...
TGH's user avatar
  • 38.9k
12 votes
3 answers
22k views

AngularJS $http call in a Service, return resolved data, not promises

I want to know if it is possible to make a service call that uses $http so it returns data directly without returning a promise? I have tried to use the $q and defer without any luck. Here is what I ...
iQ.'s user avatar
  • 3,893
12 votes
2 answers
4k views

Injecting a resolved promise into service

I need to get some information (a schema) from the server before I set up a bunch of services that depend on that information. My server provides a schema that defines various properties of a model. ...
user2463201's user avatar
11 votes
2 answers
28k views

AngularJs console.log "$q is not defined"

I am getting this error in the console $q is not defined. When I did some research I found some thing like .q library has been deprecated from http://www.breezejs.com/documentation/breeze-labs/...
RONE's user avatar
  • 5,455
11 votes
1 answer
18k views

Angular 6 Async-await not working on http request

Hi im using angular 6 to call a rest api with the code below. I am trying to acheive making the code synchronous with the async-await function. however something is missing async save() { if (...
Joshua Vandenbor's user avatar
11 votes
4 answers
9k views

Javascript: How to iterate on array using promises?

LIVE DEMO Given the following function: function isGood(number) { var defer = $q.defer(); $timeout(function() { if (<some condition on number>) { defer.resolve(); } else { ...
Misha Moroshko's user avatar

1
2 3 4 5
43