Search Posts

Category: angular-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 […]

$q in Angular or Promises in ES6

I have a project using ES6 and AngularJS, and as far as I found, AngularJS $q promises is synchronous but ES6 promises not. Right? The question is, what are the differences? and when should I use $q or ES6? I know what is the difference between Synchronous and Asynchronous, but got confused between new Promise and AngularJS $q. raise from here Source: AngularJS

Angularjs ngresource getting peoblems for promise and resolve

I have a controller and there I call a service. var rowCollection=[];‘getAllcarList’).getAllcarList({}).$promise.then(function(data) { rowCollection.push(; }); 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=’’; var factory = {}; factory.allCars= function(queryType) { var hsRESTUri […]

How to mock chained promises using Jasmine?

I am writing a unit test to a method that contains this piece of code: Name.get($scope.nameId).then(function(name){ Return name; }).then(doSomething); The function doSomething(name) looks like this. function doSomething(name){ addNametoList(name); } I don’t need to test this part of the code. Since I can’t just ignore it in my test (or can I?), I need to mock it. I mocked the first promise spyOn(mockName, ‘get’).and.returnValues($q.resolve({“Mike”})); and thought that it would propagate through the second then(doSomething) but name […]

ui-router resolve vs .run for security

I am curious as to what others are doing in regards to route security for their applications. At first we implemented our security by calling our security service from the .run method in angular ($rootScope, $state, SecuirtyService){ SecurityService.initSecurity().then(function(data){}); } SecurityService.initSecurity would then make a rest call to see what permissions the user has. The problem with this is that since .initSecurity returns a promise the rest of the application would execute until $http decided […]

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

How to wait/finish multiple insert function in for each loop?

how to do this asynchronously, print console.log(“PRINT THIS, AFTER ALL LOOP IS DONE”) i have forEach inside forEach. InsertService does not return any value, i just want to insert here. i don’t know how to apply $q and $q.all. pls help. angular.forEach(MAIN, function (value, key) { //#1 SelectService1.SelectID( (res) { angular.forEach(res, function (value, key) { InsertService1.Insert(value).then(function (res) { console.log(“NEW INSERTED Service 1!”); }, function (err) { }); }); }, function (err) { }); //#2 SelectService2.SelectID( […]

Update Angular JS1 UI when a row is updated in mysql with php on the backend

I want to update the User interface with HTML/AngularJS-1x and Php/mysql on the backend. The staging table is populated by another cpp process, I looking for ways to update the UI when that staging table is populated. I am fine with creating API or waiting using Angular Promises. But how do we proceed. One idea could be : create an get_items api which counts the staging table rows and return if the rows are there.On […]