angular-promise

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

情到浓时终转凉″ 提交于 2019-11-27 20:22:15
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 = { access: false }; this.isAuthenticated = function() { this.user = { first_name: 'First', last_name: 'Last', email: 'email@address.com', access: 'institution' }; return deferred.resolve(); }; } ]); I'm calling this in my config file via: myApp.run([ '$rootScope', 'userService', function($rootScope, userService) { return userService.isAuthenticated().then(function(response) { if

Get state of Angular deferred?

妖精的绣舞 提交于 2019-11-27 18:20:29
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 promises) Update : Due to refactoring of $q this is now possible although not documented: promise.$$state.status === 0 // pending promise.$$state.status === 1 // resolved promise.$$state.status === 2 // rejected Original : Unlike most promise libraries (Bluebird,Q, when, RSVP etc), $q does not expose a synchronous inspection API. There is no way to achieve

Possibly unhandled rejection in Angular 1.6

佐手、 提交于 2019-11-27 17:10:37
问题 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.doSomething() .then(function(result) { var x = null; var y = x.y; //do something with the result }); I'm getting clear error message: TypeError: Cannot read property 'y' of null But in version 1.6 with the same code I'm getting a different error: Possibly unhandled rejection: {} undefined I know that this is

Stop request in angularjs interceptor

狂风中的少年 提交于 2019-11-27 14:39:57
问题 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', function($q, $timeout) { var connectionInterceptor = { request: function(config) { var q = $q.defer(); $timeout(function() { q.reject(); }, 2000) return q.promise; // return config; } } return connectionInterceptor; } ]) .config(function($httpProvider) { $httpProvider.interceptors.push(

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

旧城冷巷雨未停 提交于 2019-11-27 13:16:07
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? angular.forEach(objects, function (object) { // wait for this to resolve and after that move to next object doSomething(object); }); Before ES2017 and async/await (see below for an option in ES2017), you can't use .forEach() if you want to wait for a promise because promises are not blocking. Javascript and promises just don't work that way. You can chain multiple promises and make the promise

Error handling in AngularJS http get then construct

心不动则不痛 提交于 2019-11-27 11:37:58
问题 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 function is not called. 回答1: You need to add an additional parameter: $http.get(url).then( function(response) { console.log('get',response) }, function(data) { // Handle error here }) 回答2: You can make this bit more cleaner by using: $http.get(url) .then(function

ES6 Promise not Updating AngularJS DOM [duplicate]

南笙酒味 提交于 2019-11-27 09:20:50
This question already has an answer here: Can't get ES6 promise value to display in AngularJS view 1 answer I'm having trouble understanding angular components scope. If I do something like: function myComponent(){ this.data = 'Hello World'; } let myModule = angular.module('myModule', []); myModule.component('myComponent', { template: `<div>{{$ctrl.data}}</div>`, controller: myComponent }); <script data-require="angularjs@1.5.8" data-semver="1.5.8" src="https://opensource.keycdn.com/angularjs/1.5.8/angular.min.js"></script> <div ng-app="myModule"> <my-component></my-component> </div> It prints

Angular 1.6.0: “Possibly unhandled rejection” error [duplicate]

陌路散爱 提交于 2019-11-27 07:02:31
This question already has an answer here: Possibly unhandled rejection in Angular 1.6 9 answers 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) { // pass the error the the error service return errorService.handleError(error); }); And here is how we are triggering the error in Karma: resourceMock.get = function () { var deferred = $q.defer(); deferred.reject(error); return { $promise: deferred.promise }; }; Now, with the update to

How to use the axios library with AngularJS

我的未来我决定 提交于 2019-11-27 05:38:05
Why axios callback changes are displayed in angularjs, without using $apply I was trying axios library on angularjs and I was surprised when I saw that the changes to $scope in the axios callback were detected by angular. I thought I had to call $apply like, for example, when you use setTimeout . // axios example axios.get(url).then((response) => { // Here I don't need $apply, why?? $scope.axiosResult = response.data; }); // setTimeout example setTimeout(() => { // Here I need $apply for the timeoutResult to appear on the HTML $scope.$apply(() => $scope.timeoutResult = {message: "timeout!"});

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

不想你离开。 提交于 2019-11-27 05:21:14
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() { if ($localStorage.token == undefined) { throw alert("Not logged in"); } else { Spotify.getCurrentUser().then(function(data) { var names = JSON.stringify(data.data.display_name); console.log(names) }) } }; As you can see I console.logged the name and I do get the right value in the console. But only works there if I call the function getUser() I get undefined even with a return of the names variable.