Skip to content

静态方法

ES6 扩展的静态方法:resolve、reject、try、all。这些不属于 Promise A+ 规范,但实际使用非常频繁。

本节要点

resolve:将普通值转为 Promise,如果已经是 Promise 则直接返回。 reject:返回一个 rejected 状态的 Promise。 try:尝试执行函数并包装为 Promise。 all:等待所有 Promise 完成,有一个失败则整体失败。

static resolve

static resolve
Loading code...

static reject

static reject
Loading code...

static try

static try
Loading code...

static all

static all
Loading code...

完整代码

my-promise.js — 完整实现
Loading code...

运行示例

示例一:MyPromise.resolve / reject
示例代码
Loading...
示例二:MyPromise.all 全部成功
示例代码
Loading...
示例三:MyPromise.all 有一个失败
示例代码
Loading...
示例四:MyPromise.try
示例代码
Loading...

循序渐进实现 Promise