简体   繁体   English

我可以将新的 promise 推入 Promise.all() 中间的数组吗?

[英]Can I push new promise into array in the middle of Promise.all()?

I'm trying to figure out how Promise.all works.我试图弄清楚 Promise.all 是如何工作的。 My question is: is that ok to push a new promise in the middle of Promise.all() into the same array that was passed earlier?我的问题是:可以将 Promise.all() 中间的新 promise 推送到之前传递的同一个数组中吗? For example, I have this code which works just fine: https://jsfiddle.net/artheg/z0Lndw1o/14/例如,我有这个工作得很好的代码: https://jsfiddle.net/artheg/z0Lndw1o/14/

const promises = [];
const promise1 = new Promise(function(resolve, reject) {
  resolve('promise1');
  const promise2 = new Promise(function(resolve, reject) {
    resolve('promise2');
  });
  pushPromise(promise2); // Pushing promise2 to the same array after the promise1 was resolved
});

pushPromise(promise1); // Pushing promise1

function pushPromise(promise) {
  promises.push(promise);
}

Promise.all(promises).then(function(values) {
  console.log(values); // Array [ "promise2", "promise1" ]
});

Is this behavior intended or should I always chain promises with.then() instead of pushing a new Promise into the array?这种行为是有意的还是我应该总是用.then() 链接承诺而不是将新的 Promise 推入阵列?

That's because you push the promises to the array synchronously .那是因为您将承诺同步推送到数组。 Promise.all works with the promises that are in the array by the time you pass it in. Promise.all与传入时数组中的 Promise 一起工作。

Example of a Promise added asynchronously:异步添加的 Promise 示例:

 const promises = [Promise.resolve(1)];

 setTimeout(() => promises.push(Promise.resolve(2)), 0);

 Promise.all(promises).then(console.log); // [1]

The code in a promise executor (the function you pass new Promise ) runs synchronously . promise 执行器中的代码(您传递new Promise同步运行。 So pushPromise(promise2) has already run before you call Promise.all .所以pushPromise(promise2)在你调用Promise.all之前已经运行了。 The array is already filled by the time you call Promise.all .在您调用Promise.all时,该数组已被填满。

Can I push new promise into array in the middle of Promise.all()?我可以将新的 promise 推入 Promise.all() 中间的数组吗?

Not if you want Promise.all to see that promise, no.如果您希望Promise.all看到 promise,则不是。 See the specification , Promise.all loops through the iterable you pass it synchronously, before returning, and before any handlers it ( Promise.all ) sets up on the promises can be called.请参阅规范Promise.all循环遍历您同步传递它的可迭代对象,在返回之前以及在任何处理程序之前,它( Promise.all )设置在可以调用的承诺上。

Here's an example, note that the promise from Promise.all is fulfilled rather than rejecting, even though the code adds a promise to the array that it rejects:这是一个示例,请注意,来自 Promise.all 的Promise.all已实现而不是拒绝,即使代码将 promise 添加到它拒绝的数组中:

 const promises = [ Promise.resolve(1), Promise.resolve(2).finally(() => { promises.push(Promise.reject(new Error("failed"))); }), Promise.resolve(3), ]; Promise.all(promises).then(results => { console.log(`results (${results.length}): ${results.join(", ")}`); console.log(`promises.length: ${promises.length}`); }).catch(error => { console.error(error); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM