Search Posts

Category: q

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 = […]

Showing dynamic content inside ngRepeat

Struggling to show dynamic content inside a ngRepeat. When it comes time to show my promise content, I’m getting an empty object {}: <div ng-controller=”DemoCtrl”> <div class=”sidebar” ng-repeat=”row in rows”> <div class=”row”> <input type=”checkbox”> <div class=”name”>{{row.name}}</div> <div class=”title”>{{map[$index]}}</div> </div> </div> </div> and the controller: function DemoCtrl($scope, $http, $q) { const rows = function() { const rows = []; for (let i = 0; i < 12; i++) { rows.push({ id: `demo-${i}`, name: `Demo ${i}` }); […]

Angularjs ngresource getting peoblems for promise and resolve

I have a controller and there I call a service. var rowCollection=[]; allCars.car(‘getAllcarList’).getAllcarList({}).$promise.then(function(data) { rowCollection.push(data.cars); }); console.log(rowCollection); After the service call If i want to use the rowCollection it shows undefined as the response till not come.How to stop execution until response come. So that the variable can use any where in the program. In my service I have code like this function($resource) { var apiResourceUrl=’http://127.0.0.1/stock/api/public/index.php/’; var factory = {}; factory.allCars= function(queryType) { var hsRESTUri […]

Why does a failed assertion trigger a promise catch?

While writing unit tests for an angular app I was experiencing unexpected results. I was able to condense the unexpected behavior into a sample test. The should.equal(true, false, ‘should then’) assertion failure in the then block seems to trigger the catch block of the promise. describe.only(‘test’, function () { var $q, $rootScope; beforeEach(function () { inject(function(_$q_, _$rootScope_) { $q = _$q_; $rootScope = _$rootScope_.$new(); }); }); var stubService = sinon.stub(service, ‘getPanel’); it(‘shall…’, function() { //1 […]

loop until promise function give result

I’m looking to loop on a promise function until I have the result I want. As of now I’m using recursivity like this: function pull() { dataFactory.pullFunction().then(function(res) { pull() }) } But that’s giving me some front-end/style bugs for my loading bar for example. I would do something like this: function pull() { while (res.status == ‘ONGOING’) { dataFactory.pullFunction().then(function(res) { // my stuffs }) } } But when I try it the pullFunction() is never […]