【问题标题】:nested promise reject behaviour嵌套承诺拒绝行为
【发布时间】:2021-03-14 08:21:48
【问题描述】:

我对承诺有些怀疑:

这是我使用 axios 的 api 函数:

const _get = (url: string) => axios
.get(url)
.then((response: { data: responseData }) => {
 if (response) {
   console.log(response)
    const { data, status, message } = response.data;
    if (status) {
      return data
    } else {
      throw new Error(message);
    }
  } 
})
 //notification is an antd component to show a toast with the error
.catch((error: Error) => notification.error({ message: 'Error', description: error.message }));


export const doStuff = (id: number) =>_get('/api/do/${id}');

当我调用 api 以防出错时调用 then()

  const callDoStuff = (id: number) => {
    doStuff(id).then(() => {
      //called also if doStuff catch() is resolved
      notification.success({ message: 'Success', description: 'Template deleted!' });
    });
  };

所以在 catch 块中,如果我返回的东西被认为已解决,那么外部函数 then() 会被调用吗?在这种情况下,唯一的方法是保持错误的传播并在 catch 中抛出异常?

谢谢


可能的解决方案:

const _get = (url: string) => axios
.get(url)
.then((response: { data: responseData }) => {
 if (response) {
   console.log(response)
    const { data, status, message } = response.data;
    if (status) {
      return data
    } else {
      throw new Error(message); 
    }
  } 
})

使用特定的捕获器处理 then() 错误

const callDoStuff = (id: number) => {
doStuff(id)
.then((response) => {// success handler}, e=>{// specific error thrown by the inner then })})
.catch(e=>{//axios error })

使用通用捕捉器处理错误

const callDoStuff = (id: number) => {
  doStuff(id)
  .then((response) => { //success handler })
  .catch(e=>{ // generic error handler })

【问题讨论】:

    标签: javascript promise async-await axios


    【解决方案1】:

    所以在 catch 块中,如果我返回的东西被认为已解决,那么外部函数 then() 会被调用吗?

    是的。

    在这种情况下唯一的方法是保持错误的传播并在catch中抛出异常?

    我建议不要将.catch() 放在_get 中。相反,写

    function callDoStuff(id: number) {
      doStuff(id).then(() => {
        notification.success({ message: 'Success', description: 'Template deleted!' });
      }, (error: Error) => {
        notification.error({ message: 'Error', description: error.message })
      });
    }
    

    【讨论】:

    • Ty 用于快速回放,因此: (error: Error) => { notification.error({ message: 'Error', description: error.message }) } 将用作拒绝句柄_get 函数呢?和 doStuff(id).then(() => { notification.success({ message: 'Success', description: 'Template deleted!' }); }).catch( (error: Error) => { notification.error({ message: 'Error', description: error.message }) });对吗?
    • 如果我想以通用方式处理错误,以我的方式处理错误是错误的吗?
    • 不确定您所说的“将用作 _get 函数中的拒绝句柄”是什么意思。 _get 函数对句柄一无所知,它只是拒绝它返回的承诺。 ".then(…, …).then(…).catch(…) 是否等效?" - not exactly.
    • "以我的方式处理错误是错误的吗?" - 如果您指的是把 .catch() 放在 _get 函数中,是的,这是错误的,因为它很明显不像你想要的那样工作。如果您想在任何地方使用相同的错误处理函数,只需将其设为您可以共享的命名函数即可。
    • 我有点困惑,.then(..., ...) 在这种情况下,错误回调只会捕获 _get.then() 中的拒绝,对吗?所以这样我必须复制我的代码,相反,如果我在 _get() 中放置一个 catch,那么 catch() 块也会捕获 then 块抛出的错误?
    猜你喜欢
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 2017-03-12
    • 2019-11-25
    • 2017-09-04
    • 2013-09-16
    相关资源
    最近更新 更多