Javascript 承诺拒绝不会被抓住

Javascript 承诺拒绝不会被抓住,javascript,Javascript,为什么在这里没有看到拒绝承诺?我试着调试它,但理性从来没有得到执行 axios.post('http://localhost:5000/', { contractId: contractValue }).then(() => { setLoading(false) setButton(null) }, (reason) => { console.log(reason) }) 您

为什么在这里没有看到拒绝承诺?我试着调试它,但理性从来没有得到执行

   axios.post('http://localhost:5000/', {
        contractId: contractValue
      }).then(() => {
        setLoading(false)
        setButton(null)
      }, (reason) => {
        console.log(reason)
      })

您可以添加
.catch
块来处理抛出时的所有异常,而不是将函数作为第二个参数传递

axios.post('http://localhost:5000/', {
  contractId: contractValue
}).then(() => {
  setLoading(false)
  setButton(null)
}).catch((reason) => {
  console.log(reason)
})

相反,试着在你的承诺链中加入一个
catch
。它将捕获之前链中的任何异常。它看起来像这样:

axios.post('http://localhost:5000/', {
contracted:contractValue,
})
.然后(()=>{
设置加载(错误)
设置按钮(空)
})
.catch(错误=>{
//这是处理异常的地方
console.log(错误)
})
onRejected从不处理来自同一个.then(OnCompleted)回调和.catch的拒绝承诺

const getPromise=()=>新承诺((解析,拒绝)=>{Math.round(Math.random())?解析('resolve#1'):拒绝('reject#1'))
getPromise().then(结果=>{抛出新错误('reject#2')},错误=>{//仅处理'reject#1'})
getPromise().then(result=>{throw new Error('reject#2')}).catch(Error=>{//处理'reject#1',//和'reject#2'})

以上描述来自此链接:

您确定它是拒绝的吗?只有当axios.post拒绝时,才会调用catch处理程序。
setLoading
setButton
是否抛出错误?哪个拒绝未被捕获?请提供一份报告。你的代码看起来很好。