javascript 承诺所有运行所有承诺和处理程序错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 承诺所有运行所有承诺和处理程序错误相关的知识,希望对你有一定的参考价值。
var p1 = new Promise((resolve, reject) => {
setTimeout(resolve, 1000, 'one');
});
var p2 = new Promise((resolve, reject) => {
setTimeout(resolve, 2000, 'two');
});
var p3 = new Promise((resolve, reject) => {
setTimeout(resolve, 3000, 'three');
});
var p4 = new Promise((resolve, reject) => {
setTimeout(resolve, 4000, 'four');
});
var p5 = new Promise((resolve, reject) => {
reject('reject');
});
var arrayDePromesas = [p1,p2,p5,p3,p4];
const reflect = (p,i) => p.then(
v => {
return {v, index: i, status: "fulfilled"}
},
e => {
return {e, index: i, status: "rejected" }
}
);
Promise.all(arrayDePromesas.map(reflect)).then(
(results) => {
var fulfilled = results.filter(x => x.status === "fulfilled");
var rejected = results.filter(x => x.status === "rejected");
console.log('rejected',rejected);
console.log('fulfilled',fulfilled);
});
以上是关于javascript 承诺所有运行所有承诺和处理程序错误的主要内容,如果未能解决你的问题,请参考以下文章
等待所有承诺解决
等待所有承诺解决
JavaScript 中的 Promise.all:如何获得所有承诺的解析值?
承诺等待得到解决而不返回
承诺的 JavaScript 命名约定? [关闭]
未处理的承诺拒绝,即使我很确定我已经处理了所有这些