This question already has an answer here:
I'm trying to make a function details()
that looks up the details of certain events (getdetails
) based on the available events. In the example below, details are asked for the events with IDs ZRGZZ, RGHER and GRFDZ. These details should be placed inside one array (credits to Theodore's answer). The result should be that when I call details()
, the resulting array is stored so I can use it later on.
The problem is that I don't know how I can return this final array. console.log(JSON.stringify(res));
is executed before the construction of the array is completed. I know this probably has something to do with asynchronous js, but I just can't wrap my head around it...
function details()
{
var res = {
"Result": "success",
"Key": "12345",
"Data": [{
"ID": "ZRGZZ",
"lastChangedDate": "2015-12-03 11:14:27"
}, {
"ID": "RGHER",
"lastChangedDate": "2015-12-03 15:17:47"
}, {
"ID": "GRFDZ",
"lastChangedDate": "2015-12-03 05:25:11"
}]
};
var i = 0;
var tmp;
res.Data.map(function(val,i){
getdetails(val.ID).then(function(data){
tmp = JSON.parse(data);
console.log(tmp);
Object.keys(tmp.Data[0]).map(function(v,j){
val[v] = tmp.Data[0][v];
console.log(JSON.stringify(res)); //(*)the last res gives me the result I'm looking for
});
}, function(error){ //error callback
console.log(error)
});
});
console.log(JSON.stringify(res)); //this is executed before (*)
}
Well you already use promise - see
then
in your code and if it's angular thenthen
itself return deferred promise so you can do:EDIT: inject service
$q
into your controller or servicenow when all the
getdetails
are resolved you can console.log or do whatever you want with the dataOne way is to use the async library, in particular the
async.each
orasync.eachSeries
function. (You could useasync.map
, but in your case you are actually not mapping, but modifying the underlying array items directly.)In particular, your code would look like this:
async.each
will run many iterations at the same time, whileasync.eachSeries
will run only one iteration at the same time.