联系我们
简单又实用的WordPress网站制作教学
当前位置:网站首页 > 网页前端技术 > 正文

JavaScript标准内置对象:AggregateError - Web前端

作者:98wpeu发布时间:2026-09-10分类:网页前端技术浏览:3


导读:昨天我们学习了关于“深入了解JavaScript标准内置对象:JSON.stringify()”,今天学习另外一个javascript标准内置对象:AggregateError。A...

500.jpg

昨天我们学习了关于“深入了解JavaScript标准内置对象:JSON.stringify()”,今天学习另外一个javascript标准内置对象:AggregateError。

Aggregateerror,当多个错误需要包装在一个错误中时,该对象表示一个错误。

语法

new AggregateError(errors[, message])

参数

  • errors

  • 错误的描述,默认为空。

  • message可选

  • AggregateError错误的提示信息。

描述

一个AggregateError当需要由操作报告多个错误被抛出,例如通过Promise.any(),在传递给它的所有promise拒绝。

属性

  • AggregateError.prototype

  • AggregateError的原型

AggregateError 实例

实例属性

  • AggregateError.PRototype.constructor

  • 指定创建实例原型的函数。

  • AggregateError.prototype.message

  • 错误消息,默认为""

  • AggregateError.prototype.name

  • 错误名称,默认为"AggregateError"

示例

捕获一个AggregateError

Promise.any([
  Promise.reject(new Error("some error")),
]).catch(e => {
  console.log(e instanceof AggregateError); // true
  console.log(e.message);                   // "All Promises rejected"
  console.log(e.name);                      // "AggregateError"
  console.log(e.errors);                    // [ Error: "some error" ]
});

创建一个AggregateError

try {
  throw new AggregateError([
    new Error("some error"),
  ], 'Hello');
} catch (e) {
  console.log(e instanceof AggregateError); // true
  console.log(e.message);                   // "Hello"
  console.log(e.name);                      // "AggregateError"
  console.log(e.errors);                    // [ Error: "some error" ]
}

标签:对象标准WebAggregateErrorJavaScript


网页前端技术排行
最近发表
网站分类
标签列表