Promise.reject()

Promise.reject() 静态方法返回一个因给定原因被拒绝的 Promise 对象。

¥The Promise.reject() static method returns a Promise object that is rejected with a given reason.

Try it

语法

¥Syntax

js
Promise.reject(reason)

参数

¥Parameters

reason

这个 Promise 被拒绝的原因。

返回值

¥Return value

因给定原因被拒绝的 Promise

¥A Promise that is rejected with the given reason.

描述

¥Description

静态 Promise.reject 函数返回被拒绝的 Promise。出于调试目的和选择性错误捕获,将 reason 设为 instanceof Error 很有用。

¥The static Promise.reject function returns a Promise that is rejected. For debugging purposes and selective error catching, it is useful to make reason an instanceof Error.

Promise.reject() 是通用的并且支持子类化,这意味着它可以在 Promise 的子类上调用,并且结果将是子类类型的承诺。为此,子类的构造函数必须实现与 Promise() 构造函数相同的签名 - 接受可以使用 resolvereject 回调作为参数进行调用的单个 executor 函数。Promise.reject() 本质上是 new Promise((resolve, reject) => reject(reason)) 的简写。

¥Promise.reject() is generic and supports subclassing, which means it can be called on subclasses of Promise, and the result will be a promise of the subclass type. To do so, the subclass's constructor must implement the same signature as the Promise() constructor — accepting a single executor function that can be called with the resolve and reject callbacks as parameters. Promise.reject() is essentially a shorthand for new Promise((resolve, reject) => reject(reason)).

Promise.resolve() 不同,Promise.reject() 始终将 reason 封装在新的 Promise 对象中,即使 reason 已经是 Promise 时也是如此。

¥Unlike Promise.resolve(), Promise.reject() always wraps reason in a new Promise object, even when reason is already a Promise.

示例

¥Examples

使用静态 Promise.reject() 方法

¥Using the static Promise.reject() method

js
Promise.reject(new Error("fail")).then(
  () => {
    // not called
  },
  (error) => {
    console.error(error); // Stacktrace
  },
);

用承诺拒绝

¥Rejecting with a promise

Promise.resolve 不同,Promise.reject 方法不会重用现有的 Promise 实例。它总是返回一个封装 reason 的新 Promise 实例。

¥Unlike Promise.resolve, the Promise.reject method does not reuse existing Promise instances. It always returns a new Promise instance that wraps reason.

js
const p = Promise.resolve(1);
const rejected = Promise.reject(p);
console.log(rejected === p); // false
rejected.catch((v) => {
  console.log(v === p); // true
});

在非 Promise 构造函数上调用 reject()

¥Calling reject() on a non-Promise constructor

Promise.reject() 是通用方法。可以在任何实现与 Promise() 构造函数相同签名的构造函数上调用它。例如,我们可以在将 console.log 作为 reject 传递的构造函数上调用它:

¥Promise.reject() is a generic method. It can be called on any constructor that implements the same signature as the Promise() constructor. For example, we can call it on a constructor that passes it console.log as reject:

js
class NotPromise {
  constructor(executor) {
    // The "resolve" and "reject" functions behave nothing like the
    // native promise's, but Promise.reject() calls them in the same way.
    executor(
      (value) => console.log("Resolved", value),
      (reason) => console.log("Rejected", reason),
    );
  }
}

Promise.reject.call(NotPromise, "foo"); // Logs "Rejected foo"

规范

Specification
ECMAScript Language Specification
# sec-promise.reject

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also