angular-promise

How to return single promise after for loop (which produces a promise on every iteration) is complete?

若如初见. 提交于 2019-12-05 06:09:37
I have a problem with my promise return code, I have a function getTagQuotes which contains a for loop which can make multiple calls an API to return data into an array. How my code for this begins below: // If there are tags, then wait for promise here: if (tags.length > 0) { // Setting promise var to getTagQuotes: var promise = getTagQuotes(tags).then(function() { console.log('promise =',promise); // This array should contain 1-3 tags: console.log('tweetArrayObjsContainer =',tweetArrayObjsContainer); // Loop through to push array objects into chartObj: for (var i=0; i<tweetArrayObjsContainer

angularjs handling $resource $promise errors

隐身守侯 提交于 2019-12-05 03:31:14
问题 Could somebody help me figure out how to return hard-coded data in my AngularJS factory if there is an error connecting to my API. My hard-coded data are located in another factory called "dataFactory". Appreciate the assistance. service.factory("ScheduleFactory", ['$http', '$resource', '$q', 'dataFactory', function($http, $resource, $q, dataFactory) { var objFactory = {}; objFactory.getDaysOfWeek = function(includeWeekends) { var days = []; var API = $resource(restURL + '/daysOfWeek/'); API

AngularJS then() behaves differently than success()-error() [duplicate]

社会主义新天地 提交于 2019-12-05 02:44:29
This question already has an answer here: Why are AngularJS $http success/error methods deprecated? Removed from v1.6? 2 answers As the success() and error() functions are deprecated in AngularJS, I am updating my codes, replacing them with then() . Now according to my understanding, these two pieces of codes should behave identically: $http .get(/* some params */) .success(function() { // success cases here }) .error(function() { // error cases here }); And $http .get(/* some params */) .then(function() { // success cases here }, function() { // error cases here }); But in some cases I am

How to deal the data when the promise in a loop?

十年热恋 提交于 2019-12-04 20:56:14
This is my way: var activeArr = []; var activeDate = []; var day = (endDate - startDate) / (24 * 60 * 60 * 1000); for (var i = 1; i < day + 1; i++) { activeDate.push(endDate - (24 * 60 * 60 * 1000) * i); var start = endDate - (24 * 60 * 60 * 1000) * i; var end = endDate - (24 * 60 * 60 * 1000) * (i - 1); statisService.getRegStatis(start, end).then(function(data) { activeArr.push(data); if(activeArr.length == day){ var active = []; for(var j=0;j<activeArr.length;j++){ var obj = {}; obj.date = activeDate[j]; obj.data = activeArr[j].data; active.push(obj); } $scope.active = active; } }); } the

Angular js $http factory patterns

岁酱吖の 提交于 2019-12-04 20:15:47
I've a factory named as 'userService'. .factory('userService', function($http) { var users = []; return { getUsers: function(){ return $http.get("https://www.yoursite.com/users").then(function(response){ users = response; return users; }); }, getUser: function(index){ return users[i]; } } }) In the first page, On button click I want to call getUsers function and it will return the 'users' array. I want to use 'users' array in the second page. How can I do it? P.s: I'm using getters and setters to store the response in first page and access the same in second page. Is this the way everyone

AngularJS : How to flatten this Promise chain?

余生长醉 提交于 2019-12-04 10:13:05
I have the following code: someService.fnReturnsPromise() .then(function () { return someService.fnReturnsAnotherPromise(someArg); }) .then(function (resultsOfSecondFn) { // do stuff with results }); I feel as if this should work; however, resultsOfSecondFn isn't actually the results, it's the promise itself that I returned. To make it work the way I want, I have to do this: someService.fnReturnsPromise() .then(function () { return someService.fnReturnsAnotherPromise(someArg); }) .then(function (promiseReturn) { promiseReturn.then(function (results) { // do stuff with results }); }); This is

Return a Promise that promises to throw error

两盒软妹~` 提交于 2019-12-04 06:03:07
问题 I am fairly new to using promises, I mostly stick to straight up callbacks. The following code happens to be found in an Angular Service, but that doesn't really matter that much, it's mostly regards how to create a promise that will promise to throw a certain error. So this is how it would happen with callbacks being incorporated, which avoids the problem: var client = algolia.Client('ApplicationID', 'apiKey'); var indices = { users: client.initIndex('users'), topics: client.initIndex(

AngularJS : Chaining promises

喜夏-厌秋 提交于 2019-12-04 04:54:07
问题 Following the suggestions from AngularJS validation and promises, I would like to chain confirmation dialogs and thus validate several steps at once. Based on data provided by the user, an API call is made to see what all needs to be confirmed by the user. For each step that needs confirmation, prompt the user and let them decide whether to go to next step. If any step returns false, the whole chain should return false. I've been reading a lot about async JS and promises, but I have to admit

AngularJs console.log “$q is not defined”

佐手、 提交于 2019-12-04 02:33:58
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/breezeangularqjs If this is so, then the whole concept of promises is also deprecated, Promises are not deprecated. In fact they're gaining quite a lot of momentum lately and are included in the next version of JavaScript. Let's look at what they say: This breeze.angular.q library has been deprecated. It is superseded by the Breeze Angular Service which more cleanly configures breeze for Angular

Executing then after catch

蹲街弑〆低调 提交于 2019-12-04 01:46:18
I have the following fiddle: http://jsfiddle.net/thelgevold/3uv9nnjm/6/ angular.module('hello',[]).controller('helloController',function($q){ console.clear(); function someService(){ var deferred = $q.defer(); deferred.reject({e:'error'}); return deferred.promise; } function callService(){ return someService().then(function(obj){ console.log('first then'); }). catch(function(e){ console.log('error1'); var deferred = $q.defer(); deferred.reject({e:'error'}); return deferred.promise; }); } callService().catch(function(e){ console.log('error2'); }).then(function(e){ console.log('second then'); })