Promise.all()等同于promise.then()。then()

这是代码

Promise.all([ promise1, promise2, ]) .then(() => doSomething()) 

相当于

 promise1 .then(() => promise2) .then(() => doSomething()) 

我认为它们是相同的,但是它们在财富和摩卡应用上performance不一样。 以下是关于这个应用程序的更多细节


我正在使用fortune.js ,我想用摩卡书写一些testing。 我想要实现的是使用beforeEach钩截断数据库中的表,然后插入一些preifined值。 所以,如果我有两个名为customeruser表,我会做这样的事情

 beforeEach(function () { return Promise.all([store.delete('user'), store.delete('customer')]) .then(() => store.create('customer', { id: '0987654321234567890', // More data })) .then(() => store.create('user', { id: 'qwertyuioppoiuytrewq', customer: '0987654321234567890', // More data })); }); 

这个代码不稳定,有时候工作,有时不工作,我可以find为什么(成功率约50%),

但是,如果我切换到这个代码它正在工作:

 beforeEach(function () { return store.delete('customer') .then(() => store.delete('user')) .then(() => store.create('customer', { id: '0987654321234567890', // More data })) .then(() => store.create('user', { id: 'qwertyuioppoiuytrewq', customer: '0987654321234567890', // More data })); }); 

我以为

 Promise.all([ promise1, promise2, ]) .then(() => doSomething()) 

相当于

  promise1 .then(() => promise2) .then(() => doSomething()) 

由于store.delete返回一个Promise,为什么我有不同的行为?

这个

 Promise.all([ promise1, promise2, ]) .then(() => doSomething()) 

开始同时执行这两个承诺,并在最后一次完成时调用then(),而这个

 promise1 .then(() => promise2) .then(() => doSomething()) 

从第一个承诺开始,当完成时执行第二个承诺,依此类推。

不,他们不相同。

Promise.all() 的文档声明,尽pipe返回值是inputpromise的顺序,但是它们并没有按照这个顺序被parsing。

链接承诺与.then()解决每个承诺的顺序。