angular-promise

How do I sequentially chain promises with angularjs $q?

梦想的初衷 提交于 2019-11-27 04:32:07
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)); }); return chain; however, the following doesn't work with $q : var items = ['one', 'two', 'three']; var chain = $q(); items.forEach(function (el) { chain = chain.then(foo(el)); }); return chain; Redgeoff, your own answer is the way I used to translate an array into a chained series of promises. The emergent de facto pattern is as follows : function doAsyncSeries(arr) { return arr.reduce(function

AngularJS - Promises rethrow caught exceptions

前提是你 提交于 2019-11-27 04:30:42
问题 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) return f2(); }) .then(function(data) {console.log("success 2: "+data)}) .catch(function(data) {console.log("error: "+data)}); function f1() { var deferred = $q.defer(); // An exception thrown here is not caught in catch // throw "err"; deferred.resolve("done f1"); return deferred.promise; } function f2() {

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

蓝咒 提交于 2019-11-27 03:48:24
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 !angular.equals(rowData, $scope.grid.backup[i]); }) .map(function (rowData, i) { var entityId = rowData[entityIdColumn]; return $http.put('/api/' + $scope.entityType + '/' + entityId, rowData); }); $q.all(requests).then(function (allResponses) { //if all the requests succeeded, this will be called, and $q.all will get an //array of all their responses. console.log

AngularJS promise

 ̄綄美尐妖づ 提交于 2019-11-27 03:23:02
问题 AngularJS docs say: $q promises are recognized by the templating engine in angular, which means that in templates you can treat promises attached to a scope as if they were the resulting values. So could someone please explain the reason this fiddle not working? It's not possible to change text field value. But assigning promises that $http service returns to a scope field works like a charm. Controller: function MyController($scope, $q, $timeout) { this.getItem = function () { var deferred =

Immediately return a resolved promise using AngularJS

雨燕双飞 提交于 2019-11-27 03:04:51
问题 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 want it to return, and if we haven't, we need to load the data and return a promise: this.update = function(data_loaded) { if (data_loaded) return; // We've loaded the data, no need to update var promise = Restangular.all('someBase').customGet('foo/bar').then(function(data) { // Do something with

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

自作多情 提交于 2019-11-27 01:27:53
问题 This question already has an answer here: Object method with ES6 / Bluebird promises 2 answers 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 same output for the object this console.log(this) one().then(function() { console.log(this) }) function one() { var deferred = $q.defer(); deferred

Get state of Angular deferred?

﹥>﹥吖頭↗ 提交于 2019-11-26 22:40:04
问题 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) 回答1: 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

Can't get ES6 promise value to display in AngularJS view

社会主义新天地 提交于 2019-11-26 21:55:50
问题 I'm trying to use the Pinterest JavaScript SDK to get some pin data for a project. I have a method in a Pinterest service I created that gets called in my HomeController. I tried throwing the response inside of a promise so I could put it on my HomeController's $scope and display it in my view. However, $scope.pins is undefined in my view. Why is it undefined ? Seems like the promise is working. Still learning promises. Pinterest Service function getBoardPins (id) { return new Promise

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

半世苍凉 提交于 2019-11-26 20:31:52
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 with rootScope.Scope (s). My assumptions are that when the promise resolves - it invokes the digest

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

China☆狼群 提交于 2019-11-26 20:18:48
问题 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',