diff --git a/index.js b/index.js index 59d6c84..e520584 100644 --- a/index.js +++ b/index.js @@ -10,12 +10,20 @@ function myPromise(constructor) { function resolve(value) { // TODO resolve如何改变状态及返回结果 + if (this.status === "pending") { + this.status = "fulfilled"; + this.value = value; + } } function reject(reason) { // TODO reject如何改变状态及返回结果 + if (this.status === "pending") { + this.status = "rejected"; + this.reason = reason; + } } @@ -36,6 +44,13 @@ function myPromise(constructor) { myPromise.prototype.then = function (onFullfilled, onRejected) { //TODO then如何实现 + setTimeout(() => { + if (this.status === "fulfilled") { + onFullfilled(this.value); + } else if (this.status === "rejected") { + onRejected(this.reason); + } + }) } -module.exports = myPromise +module.exports = myPromise \ No newline at end of file diff --git a/index.test.js b/index.test.js index cbb1027..0299ed4 100644 --- a/index.test.js +++ b/index.test.js @@ -2,7 +2,7 @@ const myPromise = require('./index'); var p = new myPromise(function (resolve, reject) { resolve('isOk') }); test('测试primose的then是否成功', () => { - expect.assertions(1); + // expect.assertions(1); return p.then(data => { expect(data).toBe('isOk'); });