1

I'm struggling with async functions, Axios in particular.

I got a function that passes to a WS two values (the important is 'cod') and based on the http response has to return cod or "".

function checkCod(callback){
    axiosCall.get('codecheck.php',{params:{user:usr,cod:cod}})
      .then((response)=>{
        if(response.status!==200){//if I give an error http response callback argument is ""
          console.log("false code");
          callback("");
        }else{
          callback(cod); //if everything goes right callback argument is cod (passed as the WS)
        }
      })
      .catch((error)=>{
        console.log(error);
        callback(""); //if I give an error http response callback argument is ""
      });
}

function codRes(c){
  cod=c;
  return cod;
};

return checkCod(codRes);

A value is never returned (with 200 or 500 or 400 headers, which are the 3 cases), and I really can't figure out why.

The above code is based on How do I return the response from an asynchronous call? answer...

Emi-C
  • 3,832
  • 1
  • 15
  • 15
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Lennholm Sep 05 '17 at 10:45
  • 1
    values will be available inside callback method `codRes`, do `function codRes(c){ console.log('c', c) cod=c; };` it will print the proper values, but `checkCod` will not return the values. – Mayank Shukla Sep 05 '17 at 10:51
  • 1
    You say this is based on the answer of *How do I return the response from an asynchronous call?*, but this tells me you didn't understand the issue with asynchronous functions nor how to handle them. You simply can't return from an asynchronous callback, you have to handle the result and anything that depends on it **in the callback**. – Lennholm Sep 05 '17 at 10:51
  • @MayankShukla..However we can return the result by using asynch and await in ES2017.? – MukulSharma Sep 05 '17 at 11:28
  • Thanks for the feedback, I made a small edit What then, if I want to return the new value of cod? – Emi-C Sep 05 '17 at 11:45
  • 1
    Got it, **everything** I have to do with my variable has to be done in the callback which does everything except returning values. I was struggling with that last part. Thanks again, I made it work. – Emi-C Sep 05 '17 at 12:03

1 Answers1

0

With the hints by comments I got a solution, basically I was using my "wannabe" returned value to set a state, it has to be done like this:

function checkCod(callback){
  axiosCall.get('codecheck.php',{params:{user:usr,cod:cod}})
  .then((response)=>{
    if(response.status!==200){//if I give an error http response callback argument is ""
      console.log("false code");
      callback("");
    }else{
      callback(cod); //if everything goes right callback argument is cod (passed as the WS)
    }
  })
  .catch((error)=>{
    console.log(error);
    callback(""); //if I give an error http response callback argument is ""
  });
}

function codRes(c){
  this.setState({
    cod:c
  })
  //or everything I might wanna do with my c variable resulting from async function
};
Emi-C
  • 3,832
  • 1
  • 15
  • 15