Skip to content

Promise.allSettled() ​

本节目标:理解“Promise.allSettled()”的核心思路,并能把它用于实际开发或面试表达。

Promise.allSettled()方法接受一组 Promise 实例作为参数,包装成一个新的 Promise 实例。只有等到所有这些参数实例都返回结果,不管是fulfilled还是rejected,包装实例才会结束。
该方法由 [ES2020](https://github.com/tc39/proposal-promise-allSettled) 引入。
const promises = [  fetch('/api-1'),  fetch('/api-2'),  fetch('/api-3'),];
await Promise.allSettled(promises);removeLoadingIndicator();
上面代码对服务器发出三个请求,等到三个请求都结束,不管请求成功还是失败,加载的滚动图标就会消失。
返回值:该方法返回的新的 Promise 实例,一旦结束,状态总是fulfilled,不会变成rejected。状态变成fulfilled后,Promise 的监听函数接收到的参数是一个数组,每个成员对应一个传入Promise.allSettled()的 Promise 实例。
const resolved = Promise.resolve(42);const rejected = Promise.reject(-1);
const allSettledPromise = Promise.allSettled([resolved, rejected]);
allSettledPromise.then(function (results) {  console.log(results);});// [//    { status: 'fulfilled', value: 42 },//    { status: 'rejected', reason: -1 }// ]
上面代码中,Promise.allSettled()的返回值allSettledPromise,状态只可能变成fulfilled。它的监听函数接收到的参数是数组results。该数组的每个成员都是一个对象,对应传入Promise.allSettled()的两个 Promise 实例。每个对象都有status属性,该属性的值只可能是字符串fulfilled或字符串rejected。fulfilled时,对象有value属性,rejected时有reason属性,对应两种状态的返回值。
下面是返回值用法的例子。
const promises = [ fetch('index.html'), fetch('https://does-not-exist/') ];const results = await Promise.allSettled(promises);
// 过滤出成功的请求const successfulPromises = results.filter(p => p.status === 'fulfilled');
// 过滤出失败的请求,并输出原因const errors = results  .filter(p => p.status === 'rejected')  .map(p => p.reason);
有时候,我们不关心异步操作的结果,只关心这些操作有没有结束。这时,Promise.allSettled()方法就很有用。如果没有这个方法,想要确保所有操作都结束,就很麻烦。Promise.all()方法无法做到这一点。
const urls = [ /* ... */ ];const requests = urls.map(x => fetch(x));
try {  await Promise.all(requests);  console.log('所有请求都成功。');} catch {  console.log('至少一个请求失败,其他请求可能还没结束。');}
上面代码中,Promise.all()无法确定所有请求都结束。想要达到这个目的,写起来很麻烦,有了Promise.allSettled(),这就很容易了。

本站总访问 — 次