function MYPromise(fn) { var self = this self.onResolvedCallback = [] function resolve(value) { setTimeout(() => { self.data = value self.onResolvedCallback.forEach(callback => callback(value)) }) } fn(resolve) } MYPromise.prototype.then = function (fn) { var self = this return new MYPromise(resolve => { self.onResolvedCallback.push(function () { var result = fn(self.data) if (result instanceof MYPromise) { result.then(resolve) } else { resolve(result) } }) }) }
参考文章:https://juejin.im/post/5e6f4579f265da576429a907#heading-0