实现一个类Pormise
核心代码
为了精简代码,下面的Promise实现中去掉了代码校验,这样方便小伙伴看到Promise的核心逻辑。ES6语法。活不多说,直接放代码
class PromiseTest { executor = (resolve, reject) => { } constructor(executor) { this.executor = executor; } then(sucess, error) { let flag = true; let resolveV; let rejectV; const resolve = (value) => { flag = true; resolveV = sucess(value); }; const reject = (value) => { flag = false; rejectV = error(value); }; this.executor(resolve, reject); // onfulfilled onrejected返回参数的传递 这是promise的精髓