-2

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?

Community
  • 1
  • 1
lightweight
  • 3,227
  • 14
  • 79
  • 142
  • Are you sure you have gone through the question you referred? It's a long answer that explains everything there. PS: as a side suggestion - learn how to indent, at the moment it's near to impossible to see the hierarchy in your code. – zerkms Nov 16 '16 at 20:15
  • yes...I'm specifically following the "If you're not using jQuery in your code, this answer is for you" response...I can't seem to figure out whats wrong...please note that I'm a n00b at javascript so I'm trying to learn as I go... – lightweight Nov 16 '16 at 20:16
  • Not sure why you mentioned jquery at all, please re-read the checked answer once again. Entirely. – zerkms Nov 16 '16 at 20:18
  • @zerkms thats the title of the response, right? "If you are not using jQuery.." which I'm not, so shouldn't I follow that one? – lightweight Nov 16 '16 at 20:21
  • If you have read the answer entirely - you would have known it has nothing to do with jquery. It's unclear what kind of help you're looking for if you are lazy to read the link that contains the answer for you. – zerkms Nov 16 '16 at 20:22
  • @zerkms I know it doesn't have anything to do with jQuery....Im just trying to follow the guide that has jQuery in the title in the referenced question...I'm not referrring to jQuery, the question in the link has a title that has the word jQuery in it...I'm trying to be as specific as possible on what i"m doing and what I'm referring to – lightweight Nov 16 '16 at 20:23
  • How so? What exactly have you done as per that advice? – zerkms Nov 16 '16 at 20:24
  • 2
    The point is that your code is nothing like what the answer you're referring to would instruct you to do, so it's really hard to say anything other than "read the answer again." – JJJ Nov 16 '16 at 20:28
  • I'm following this part of the question: http://stackoverflow.com/a/16825593/2061886..and specifically the part where it says `One possible solution to this problem is to code re-actively `...I'm structured my code to follow that but am not getting the same behavior. – lightweight Nov 16 '16 at 20:28
  • 1
    "I'm structured my code to follow that but am not getting the same behavior." --- no you have not. See that they don't use `return`, at all. And never use any returned values anywhere. – zerkms Nov 16 '16 at 20:29
  • 1
    I'm tempted to vote to close this as a duplicate of http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call. I mean, it's the same question and the code has literally the same problem. – melpomene Nov 16 '16 at 20:31
  • I made an edit to show what I'm following...if I'm following the wrong part...let me know and I'll delete this question – lightweight Nov 16 '16 at 20:32
  • Your problem is that (for some reason) you're looking at the return value of `maxYvalue2`. That's pointless because it returns `undefined`. – melpomene Nov 16 '16 at 20:33
  • The main idea of software engineering is not that you're following some instructions/guides/tutorials. You need to understand *the problem*, then understand how the solution works, then to apply the knowledge for your particular case. – zerkms Nov 16 '16 at 20:33
  • Maybe you've confused yourself with your choice of identifiers. You have a function called `maxYvalue2`, two different variables called `maxYvalue`, a variable called `maxYvalue1`, and a variable called `mxVal`. Half of these are globals (for no good reason). – melpomene Nov 16 '16 at 20:36

1 Answers1

2

Promises don't make code synchronous. You'll never be able to immediately return the value from maxYvalue2. Just return the promise instead:

function maxYvalue2() {
    return Rpt_scn_cost_v.find({filter: { where: {scenario_id: $stateParams.id}}}).$promise.then(function(response){
        var maxYvalue = 0
        for (var i=0;i<response.length;i++) {
            var currMaxYvalue = parseFloat(response[i].cur_cost) + parseFloat(response[i].tgt_cost);
            if (currMaxYvalue > maxYvalue) {
                maxYvalue = currMaxYvalue
            };
        }
        console.log("y3: " + maxYvalue)
        return maxYvalue;
    });
}

maxYvalue2().then(function onComplete(yVal) {
    console.log("fnc: " + yVal);
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375