实现符合Promises/A+规范的Promise库需处理状态机、异步解析和链式调用。1. 定义三种不可逆状态:pending、fulfilled、rejected;2. 构造函数中通过resolve/reject控制状态流转并存储回调;3. then方法返回新Promise,根据当前状态异步执行对应回调,并使用queueMicrotask确保异步执行;4. resolvePromise函数处理返回值x,判断是否为thenable或Promise实例,避免循环引用,保证正确决议新Promise。该结构支持链式调用与错误传递,符合核心规范要求。

实现一个符合 Promises/A+ 规范的 Promise 库,核心在于理解并正确处理状态机、异步解析流程和 then 方法的链式调用。下面是一个简化但符合规范关键点的实现思路与代码结构。
Promise 有三种状态:
状态一旦变更,就不能再次修改。then 方法根据当前状态决定执行 onFulfilled 还是 onRejected 回调,并支持异步延迟绑定。
定义构造函数和内部状态:
function MyPromise(executor) {
this.state = 'pending';
this.value = undefined;
this.reason = undefined;
this.onFulfilledCallbacks = [];
this.onRejectedCallbacks = [];
const resolve = (value) => {
if (this.state === 'pending') {
this.state = 'fulfilled';
this.value = value;
this.onFulfilledCallbacks.forEach(fn => fn());
}
};
const reject = (reason) => {
if (this.state === 'pending') {
this.state = 'rejected';
this.reason = reason;
this.onRejectedCallbacks.forEach(fn => fn());
}
};
try {
executor(resolve, reject);
} catch (err) {
reject(err);
}
}
then 方法必须返回一个新的 Promise,以支持链式调用。这是 Promises/A+ 的重点。
MyPromise.prototype.then = function(onFulfilled, onRejected) {
// 处理回调可选的情况
onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : val => val;
onRejected = typeof onRejected === 'function' ? onRejected : err => { throw err; };
// 返回新 Promise 实现链式调用
const promise2 = new MyPromise((resolve, reject) => {
if (this.state === 'fulfilled') {
queueMicrotask(() => {
try {
const x = onFulfilled(this.value);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
});
}
if (this.state === 'rejected') {
queueMicrotask(() => {
try {
const x = onRejected(this.reason);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
});
}
if (this.state === 'pending') {
this.onFulfilledCallbacks.push(() => {
queueMicrotask(() => {
try {
const x = onFulfilled(this.value);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
});
});
this.onRejectedCallbacks.push(() => {
queueMicrotask(() => {
try {
const x = onRejected(this.reason);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
});
});
}
});
return promise2;
};
这个函数用于处理 onFulfilled/onRejected 返回值 x 的情况,判断是否为 Promise 实例或 thenable 对象。
function resolvePromise(promise2, x, resolve, reject) {
if (promise2 === x) {
return reject(new TypeError('Chaining cycle detected'));
}
let called = false;
if (x != null && (typeof x === 'object' || typeof x === 'function')) {
try {
const then = x.then;
if (typeof then === 'function') {
then.call(x, y => {
if (called) return;
called = true;
resolvePromise(promise2, y, resolve, reject);
}, r => {
if (called) return;
called = true;
reject(r);
});
} else {
resolve(x);
}
} catch (e) {
if (called) return;
called = true;
reject(e);
}
} else {
resolve(x);
}
}
基本上就这些。以上实现涵盖了 Promises/A+ 的核心要求:状态不可逆、then 可链式调用、兼容 thenable、处理循环引用等。要完全通过官方测试套件(promises-aplus-tests),还需补充一些边界情况,比如参数校验、错误冒泡、多次 resolve/reject 防止重复调用等。
以上就是如何实现一个符合Promises/A+规范的Promise库?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号