Javascript Axios似乎绕过了这两者。然后,/.catch被触发。最后直接触发
我在Axios(0.19.0)上使用React(16.13.1),遇到了一个奇怪的情况 设置了多个Javascript Axios似乎绕过了这两者。然后,/.catch被触发。最后直接触发,javascript,reactjs,axios,Javascript,Reactjs,Axios,我在Axios(0.19.0)上使用React(16.13.1),遇到了一个奇怪的情况 设置了多个console.log(),以尝试解决问题 const signUserAPI = async (data) => { console.log('top of the function') await setIsFetching(true); await setIsError(''); axios.post( apiPath.signUpPat
console.log()
,以尝试解决问题
const signUserAPI = async (data) => {
console.log('top of the function')
await setIsFetching(true);
await setIsError('');
axios.post(
apiPath.signUpPath,
{ user: data },
).then((response) =>{
console.log('then')
console.log(response)
setIsSuccess(true)
}
).catch((e) => {
console.log('catch')
setIsSuccess(false);
setIsError(e);
if (e.response) {
...
}
}).finally(() => setIsFetching(false), console.log('finally'));
};
当启动axios.post
时,我应该按照以下顺序使用console.log()获取内容
// in console
'top of the function'
'then'
'response content...'
'finally'
// OR
'top of the function'
'catch'
'finally'
但我真正得到的是
// at 1st render
'top of the function'
'finally'
// at 2nd render
'then'
'response content ...'
// OR
// at 1st render
'top of the function'
'finally'
// at 2nd render
'catch'
看起来axios在第一次渲染时绕过了然后
或捕获
,直接进入了最终
,在第二次渲染时绕过了最终
有没有人有同样的经历或者能向我解释发生了什么
我真的很感激。试着正确地编写您的
最后的回调,即
() => {
setIsFetching(false);
console.log('finally');
}
写入时,两行都会执行,但console.log会立即执行,并且不会包含在回调中。它是一个逗号分隔的表达式列表,所有表达式都将被计算并返回最后一个表达式
最后一个块回调问题示例
(setTimeout(()=>console.log('hi'),2000),console.log('test'))
尝试正确地编写最后的回调,即
() => {
setIsFetching(false);
console.log('finally');
}
写入时,两行都会执行,但console.log会立即执行,并且不会包含在回调中。它是一个逗号分隔的表达式列表,所有表达式都将被计算并返回最后一个表达式
最后一个块回调问题示例
(setTimeout(()=>console.log('hi'),2000),console.log('test'))
您在finally块中写入了错误的cod,请使用以下更改
const signUserAPI = async (data) => {
console.log('top of the function')
await setIsFetching(true);
await setIsError('');
axios.post(
apiPath.signUpPath,
{ user: data },
).then((response) =>{
console.log('then')
console.log(response)
setIsSuccess(true)
}
).catch((e) => {
console.log('catch')
setIsSuccess(false);
setIsError(e);
if (e.response) {
...
}
}).finally(() =>{ setIsFetching(false); console.log('finally');});
};
您在finally块中写错了cod,请使用下面的更改
const signUserAPI = async (data) => {
console.log('top of the function')
await setIsFetching(true);
await setIsError('');
axios.post(
apiPath.signUpPath,
{ user: data },
).then((response) =>{
console.log('then')
console.log(response)
setIsSuccess(true)
}
).catch((e) => {
console.log('catch')
setIsSuccess(false);
setIsError(e);
if (e.response) {
...
}
}).finally(() =>{ setIsFetching(false); console.log('finally');});
};
哦我懂了。原谅我,我是这个领域的新手。非常感谢你的解释,德鲁·里斯@FahnLiu不用担心,如果这足以解决您的问题,请接受,如果您感谢解释,请投票。哦。。。我懂了。原谅我,我是这个领域的新手。非常感谢你的解释,德鲁·里斯@FahnLiu不用担心,如果这足以解决您的问题,请接受,如果您感谢解释,请投票。明白了!谢谢你,Mahamudul HasanGot博士!谢谢你,Mahamudul Hasan博士