Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t返回带有回调的异步值_Javascript_Reactjs_Asynchronous_Axios - Fatal编程技术网

Javascript Can';t返回带有回调的异步值

Javascript Can';t返回带有回调的异步值,javascript,reactjs,asynchronous,axios,Javascript,Reactjs,Asynchronous,Axios,我正在努力使用异步函数,尤其是Axios 我得到了一个传递给WS的函数,它有两个值(重要的是'cod'),并且基于http响应必须返回cod或“” 一个值永远不会返回(有200或500或400个头,这是3种情况),我真的不明白为什么 上面的代码基于答案…和注释提示我得到了一个解决方案,基本上我是使用我的“wannabe”返回值来设置状态的,必须这样做: function checkCod(callback){ axiosCall.get('codecheck.php',{params:{us

我正在努力使用异步函数,尤其是Axios

我得到了一个传递给WS的函数,它有两个值(重要的是'cod'),并且基于http响应必须返回cod或“”

一个值永远不会返回(有200或500或400个头,这是3种情况),我真的不明白为什么


上面的代码基于答案…

和注释提示我得到了一个解决方案,基本上我是使用我的“wannabe”返回值来设置状态的,必须这样做:

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
};

通过注释的提示,我得到了一个解决方案,基本上我是在使用我的“wannabe”返回值来设置状态,必须这样做:

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
};

回调方法
codRes
,do
函数codRes(c){console.log('c',c)cod=c;}中可能存在重复的值
它将打印正确的值,但
checkCod
不会返回值。您说这是基于如何从异步调用返回响应的答案,但这告诉我您不了解异步函数的问题,也不知道如何处理它们。您根本无法从异步回调返回,您必须在回调中处理结果以及依赖于它的任何内容。@MayankShukla..但是我们可以使用asynch返回结果,并在ES2017中等待。?明白了,我必须在回调中处理变量的所有操作,回调除了返回值之外,其他操作都在回调中完成。我在为最后一部分而挣扎。再次感谢,我让它工作了。回调方法
codRes
,do
函数codRes(c){console.log('c',c)cod=c;}中可能有重复的值
它将打印正确的值,但
checkCod
不会返回值。您说这是基于如何从异步调用返回响应的答案,但这告诉我您不了解异步函数的问题,也不知道如何处理它们。您根本无法从异步回调返回,您必须在回调中处理结果以及依赖于它的任何内容。@MayankShukla..但是我们可以使用asynch返回结果,并在ES2017中等待。?明白了,我必须在回调中处理变量的所有操作,回调除了返回值之外,其他操作都在回调中完成。我在为最后一部分而挣扎。再次感谢,我成功了。