然后,promise.then.then和promise.then之间有区别吗?然后答应 [英] Is there a difference between promise.then.then vs promise.then; promise.then

查看:300
本文介绍了然后,promise.then.then和promise.then之间有区别吗?然后答应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道以下两个之间有区别吗?

I want to know is there a difference between following two?

  1. aPromiseObj.then(fn1).then(fn2).catch(fn3);
  2. aPromiseObj.then(fn1); aPromiseObj.then(fn2); aPromiseObj.catch(fn3);
  1. aPromiseObj.then(fn1).then(fn2).catch(fn3);
  2. aPromiseObj.then(fn1); aPromiseObj.then(fn2); aPromiseObj.catch(fn3);

工作流程会改变吗?

ps:尽管我想更广泛地考虑这一点,但我仍处在棱角分明的环境中.

ps: I am in angular environment, though I would like to think this in a broader term.

推荐答案

您问过链接" 分支" .

假设f1f2表示返回诺言的异步操作,是的,两者之间存在显着差异.对于选项1:

Assuming that f1 and f2 represent asynchronous operations that return promises, yes there is a significant difference. For option 1:

  1. 它对fn1fn2进行序列化,以便在解决fn1返回的承诺后才调用fn2.
  2. .catch()适用于fn1fn2中的错误,或者aPromiseObj拒绝的情况.
  3. 如果fn1拒绝,则不会调用
  4. fn2.
  1. It serializes fn1 and fn2 so that fn2 is not called until after the promise returned by fn1 has been resolved.
  2. .catch() applies to an error in either fn1 or fn2 or if aPromiseObj rejects.
  3. fn2 will not be called if fn1 rejects.

对于选项2:

  1. fn2不等待fn1解析.一旦fn1返回类似于fn1(); fn2();的内容,就会调用fn2.这意味着由fn1fn2启动的异步操作将同时进行(有时称为并行运行而不是串行运行).
  2. .catch()也不适用,因为它不在任何.then()调用所创建的promise上.选项2中的.catch()仅适用于aPromiseObj拒绝的情况,不适用于f1()f2().
  3. fn1fn2都将被调用,而不管两者中是否有错误.
  1. fn2 does not wait for fn1 to resolve. fn2 is called as soon as fn1 returns similar to fn1(); fn2();. This means the async operations started by fn1 and fn2 will both be in-flight at the same time (sometimes referred to running in parallel instead of running serially).
  2. The .catch() does not apply to either because it is not on the promise that is created by either of the .then() calls. The .catch() in option 2, only applies to if aPromiseObj rejects, not f1() or f2().
  3. Both fn1 and fn2 will be called regardless of an error in either.

另一个相关的问题/答案:了解JavaScript承诺;堆栈和链接

Another related question/answer: Understanding javascript promises; stacks and chaining

这篇关于然后,promise.then.then和promise.then之间有区别吗?然后答应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆