I'm trying to follow the async question/guide on this page, specifically the answer that is "If you're not using jQuery in your code, this answer is for you" in this question: How do I return the response from an asynchronous call? but I can't seem to get the value to return to the var
.
function maxYvalue2(whendone) {
Rpt_scn_cost_v.find({
filter: {
where: {
scenario_id: $stateParams.id
}
}
}).$promise.then(function(response) {
var maxYvalue = 0
for (i = 0; i < response.length; i++) {
currMaxYvalue = parseFloat(response[i].cur_cost) + parseFloat(response[i].tgt_cost);
if (currMaxYvalue > maxYvalue) {
maxYvalue = currMaxYvalue
};
}
console.log("y3: " + maxYvalue)
whendone(maxYvalue);
return maxYvalue;
});
return maxYvalue;
};
function onComplete(maxYvalue1) {
mxVal = maxYvalue;
console.log("mx: " + mxVal)
return mxVal;
};
var yVal = maxYvalue2(onComplete);
console.log("fnc: " + yVal);
but yVal
is still showing up as undefined...I followed the previous question/answer but still can't get the output thats in the guide....
I'm trying to follow this structure mentioned in the code:
function onComplete(a){ // When the code completes, do this
alert(a);
}
function getFive(whenDone){
var a;
setTimeout(function(){
a=5;
whenDone(a);
},10);
}
and then call it like this:
getFive(onComplete);
am I following the right part of the response in the reference question?