Search Posts

Category: promise

Return an array from a function with a promise

I need to return the array lotSpecItems from this function once it is populated: public getLotSpecItems(id: number) { let toReturn = this.resourceClass.getLotSpecItems({ lotId: id }); let lotSpecItems = []; return toReturn.$promise.then(lotSpecItemsArray => { lotSpecItemsArray.forEach(lotSpecItem => { lotSpecItems.push(this.$di.logicFactory.getLotSpecLogic().mapLotSpecItem(lotSpecItem)); }); }); } The $promise is an angular js promise: (property) angular.resource.IResourceArray<any>.$promise: ng.IPromise<ng.resource.IResourceArray<any>> Client code (can be edited to not expect a promise): $onInit() { this.lotLogic.getLotSpecItems( => { this.searchResults = results; console.log(this.searchResults); }); It currently returns undefined. What […]

AngularJS: promise in a loop

I am unable to do the promise looping. I make a service call to get list of providers, then for each provider, I make another service call to get a customer. A provider has 1 or more customers. So eventual list of customer is to be decorated and displayed. In other format I am trying to achieve: *serviceA.getProvider(){ –foreah(providers){ —foreach(provider.customerID){ —-serviceB.getCustomer(customerId) —} –} } .then( –foreach(Customer){ —updateTheCustomer; —addUpdatedCustomerToAList –} displayUpdatedCustomreList(); )* I have written following […]

Automating promises in a service

I have some very inefficient code in a service that I would like to streamline. var dayPromise = DayResource.query({ start: dateQueryParam, end: dateQueryParam }).$promise.then(function(response) { if (response.length) { response = response[0]; MealLogResource.query({ … }, function(mealLog) { mealLogDeferred.resolve(mealLog); }); … … … } var dayBack1Promise = DayResource.query({ start: dateQueryParamBack1, end: dateQueryParamBack1 }).$promise.then(function(response) { if (response.length) { response = response[0]; MealLogResource.query({ … }, function(mealLog) { mealLogDeferredBack1.resolve(mealLog); }); } else { // Instantiating new empty day response = […]

How to use .then method for vcRecaptchaService.execute()?

I am using anuglar-recaptcha for adding Google Invisible Recaptcha to my login form The issue I am facing is, immediately after I call vcRecaptchaService.execute, if I print the recaptcha_reponse I will see undefined as output. But if I print recaptcha_reponse few seconds afterwards by using a test function printRecaptchaReponse I will see the recaptcha_reponse getting printed. The Issue here is, the library function vcRecaptchaService.execute() is not returning a $promise here. Its actually returning none Is […]

forEach not hitting last index’s

Hello I’m looping through an array within my Angular Controller and passing a Promise function to each value, then dynamically creating a $scope module for each one labeled “$scope[‘index_0]” for example. app.controller(‘arrCtrl’, function($scope, dataService){ var catList = <%-JSON.stringify(myCats)%>; var sequence = Promise.resolve(); catList.forEach(function(catObj, index){ sequence = sequence.then(function(){ return dataService.getItemsByCategory(catObj._id); }).then(function(result){ //Create and set scope module to Items array $scope[‘items_’+index] = result; }).catch(function(err){//catch any error console.log(err + ‘ failed to load!’); }); }); console.log(“Controller loaded”); }); […]

Promise not returning to controller in Angular

I am injecting $q into a factory in order to handle all related methods and callbacks from server. I have a method to add data to mongoDB database, is working all fine I receive the positive answer and I resolve the promise but is not returning back to the controller In the controller: $scope.addArticle = function(){ console.log(‘add article route working’); ArticleService.addArticle($scope.article.title, $scope.article.content, $scope.article.url) .then(function(){ refresh(); // here is where I am expecting it to return […]

Return extended object in .map function from service to controller as promise

What I am trying to get done is extend JSON object in service and then pass it to controller. JSON came to service from another service which makes backend call. The code is pretty complicated so I add comments and console.logs: // prepare name of games icons. This is support function executed in next method transformSpace: function(subject) { var ensuredSubject = subject.toString().toLowerCase(); var transformedSubject = ensuredSubject.replace(/ /g, ‘_’); return transformedSubject; }, //add iconname property to […]

angular $q and google.maps.api

I’m struggleling since hours trying to understand how is working $q. I’m trying to make it works with a function from the google.maps api but I’m definitely not able to answer it by myself or maybe I totally don’t understand $q. I hope you’ll be able to help me or at least give me some explanation. Here is a very simple example of what I would like to do : var geocoder = new google.maps.Geocoder(); […]

How to make JavaScript promise wait for another promise to be resolved inside for() loop

I have an AngularJS application, which I use promises to get data from firebase database. Here is my home-controller: $scope.wallets; walletDAO.getWalletsByUserId(auth.uid) .then(function(wallets){ $scope.wallets = wallets; $scope.$apply(); }) .catch(function(error){ console.log(error); }); These are my two methods inside an service I call walletDAO: this.getWalletsByUserId = function(id) { return new Promise(function(resolve, reject) { //codigo aqui var dbRef = database.ref(“users/” + auth.currentUser.uid); dbRef.on(‘value’, function(data) { //console.log(“Wallet IDs Retrieved!”); var userOnDB = data.val(); var walletIds = userOnDB.wallets; var wallets = […]

Call back function returns an object which cannot be accessed ,it only appears in console.log in angular

I have followed the below git hub link that has a service called and using this service in login function as (googleService) function login () { googleService.login().then(function (data) { $‘/googleSignIn’,data) .then(function (data) { // window.location.reload(); }, function (err) { if ( { toastr.error(; return; } }); }, function (err) { console.log(‘Failed: ‘ + err); }); }; The data object can be seen in console.log statment only when I click the arrow beside it and […]

Next Page »