Reputation: 11
function getTotal.getValues() is a server call that returns "one", "two", "three" ... "nine". i can print them with console.log(res). however, it seems i cannot push them into variable v created inside runTest Function. in this code, console.log(r) does not print anything because return v is empty. any ideas?
var test = [1,2,3,4,5,6,7,8,9];
function runTest(val) {
var v = [];
val.forEach(function(t) {
getTotal.getValues(t).then(function(res) {
//console.log(res);
v.push(res);
});
});
return v;
}
runTest(test).forEach(function(r) {
console.log(r);
});
Upvotes: 0
Views: 1679
Reputation: 4157
This is how your forEach function should look like:
var test = [1,2,3,4,5,6,7,8,9];
test.forEach(function logArrayElements(element, index, array) {
console.log("a[" + index + "] = " + element);
});
// logs:
// a[0] = 1
// a[1] = 2
// a[2] = 3
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
But if you want to use angular.forEach(), refer to: https://docs.angularjs.org/api/ng/function/angular.forEach
Good luck!
Upvotes: 0