JS-内部和外部Catch

huangapple go评论71阅读模式
英文:

JS- Inner and Outer Catch

问题

有两个catch,像这样:

try {
  axios.get('https://example.com', {})
    .then(function (responseOne: any) {
      return axios.post('https://example.com', {}, {})
    }).then(function (responseTwo: any) {
      return axios.put('https://example.com', {}, {})
    }).then(function (responseThree: any) {
      return axios.post('https://example.com', {}, {})
    }).then(function () {
      reply.send
    }).catch(error) => {}
} catch(errorOuter) => {}

根据我的了解,只有在第一个axios调用是await axios.get(...)时,它才会进入外部的catch。这样的理解是否正确?如果每个then块也是await,那会怎样?

如果代码保持原样,它是否只会进入内部的catch。如果是这样,我想我可以删除外部的catch。

英文:

Say I have 2 catchs. like so:

try {
axios.get('https://example.com', {})
  .then(function (responseOne: any) {
    return axios.post('https://example.com', {}, {})
  }).then(function (responseTwo: any) {
    return axios.put('https://example.com', {}, {})
  }).then(function (responseThree: any) {
    return axios.post('https://example.com', {}, {})
  }).then(function () {
    reply.send
  }).catch(error) => {}
} catch(errorOuter) => {}

From what I've read - it would only go into the outer catch the first axios call was await axios.get(...). Is this correct? What if each of the then blocks was also await?

If the code remains as is, would it only ever go into the inner catch. And if so, I guess I can remove the outer catch.

答案1

得分: 2

Not really.

The promise returned by axios.get('https://example.com', {}) is handled by then, and the same is true for the subsequence promises up until the end of catch.

If you added await at the start of line 2 then you would be awaiting the promise returned by the call to catch().

The callback you pass to catch() captures and discards any exception anywhere on that chain of promises.

Thus it would never go into the outer catch.


Technically, yes, but you'd be better off getting rid of then and just using await. It would make the code easier to follow.

try {
    await axios.get('https://example.com', {});
    await axios.post('https://example.com', {}, {});
    await axios.put('https://example.com', {}, {});
    await axios.post('https://example.com', {}, {});
    reply.send;
} catch (e) {
    // do something with e
}
英文:

> it would only go into the outer catch the first axios call was await axios.get(...). Is this correct?

Not really.

The promise returned by axios.get('https://example.com', {}) is handled by then, and the same is true for the subsequence promises up until the end of catch.

If you added await at the start of line 2 then you would be awaiting the promise returned by the call to catch().

The callback you pass to catch() captures and discards any exception anywhere on that chain of promises.

Thus it would never go into the outer catch.


> If the code remains as is, would it only ever go into the inner catch. And if so, I guess I can remove the outer catch.

Technically, yes, but you'd be better off getting rid of then and just using await. It would make the code easier to follow.

try {
    await axios.get('https://example.com', {});
    await axios.post('https://example.com', {}, {});
    await axios.put('https://example.com', {}, {});
    await axios.post('https://example.com', {}, {});
    reply.send;
} catch (e) {
    // do something with e
}

答案2

得分: 1

在这个特定的例子中,我没有看到任何方式,可以调用与原始的try匹配的“外部”catch,除非某种方式Axios在尝试调用其异步操作时抛出异常。

对于每个后续操作的链接的Promise最终将在该Promise中链接的.catch()调用中结束,如果某些事情出错的话。但是整个链是一个大的异步操作,整个try块在该操作到达任何地方之前完成并退出。

(也就是说...如果在显示的所有代码之后立即添加一个console.log语句,你会看到该语句在任何AJAX操作返回之前被执行。)

在这种情况下的“外部”try/catch只有在你从链接.then()回调切换到使用await时才有用。例如:

try {
  const responseOne = await axios.get('https://example.com', {});
  const responseTwo = await axios.post('https://example.com', {}, {});
  const responseThree = await axios.put('https://example.com', {}, {});
  // 等等。
} catch (errorOuter) {
  // 处理错误
}
英文:

In this particular example I don't see any way that the "outer" catch (the one matched to the original try) would be invoked, unless somehow Axios threw an exception just trying to invoke its asynchronous operation.

The chained promises for each subsequent operation will eventually land in the chained call to .catch() in that Promise if something failed. But the whole chain is one big asynchronous operation, and that overall try block completes and exits before that operation ever gets anywhere.

(That is... If you were to add a console.log statement immediately after all of the code shown, you'd see that statement is reached before any of the AJAX operations have returned.)

The "outer" try/catch in this case would only be useful if you switched from chaining .then() callbacks to using await. For example:

try {
  const responseOne = await axios.get('https://example.com', {});
  const responseTwo = await axios.post('https://example.com', {}, {});
  const responseThree = await axios.put('https://example.com', {}, {});
  // etc.
} catch(errorOuter) {
  // handle error
}

huangapple
  • 本文由 发表于 2023年3月7日 21:49:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定