Promise.allSettled polyfill

Promise.allSettled = (promises) => {  
  const wrappedPromises = promises.map(p => Promise.resolve(p)
    .then(
      val => ({ status: 'fulfilled', value: val }),
      err => ({ status: 'rejected', reason: err })
    )
  );
  return Promise.all(wrappedPromises);
}

Test:

Promise.allSettled([  
  Promise.reject(new Error('reason')),
  new Promise(r => setTimeout(r, 1000))
])
  .then(console.log)
  .catch(console.error)
// < Promise {<pending>}
// after 1s
// > [{"status":"rejected","reason":'Error: reason at <anonymous>:2:18'},{"status":"fulfilled"}]
正在加载 Disqus 评论组件...