1. 明确需求

一个 Promise 应该具备的最基本的特征,至少有以下几点:

  • 可以接收一个 executor 作为入参
  • 具备 pending、resolved 和 rejected 这三种状态

2. 代码编写

1. 基本轮廓

  1. const PENDING = 'pending';
  2. const FULFILLED = 'fulfilled';
  3. const REJECTED = 'rejected';
  4. function Promise1(execute) {
  5. // 保存this
  6. let self = this
  7. // status记录当前状态,设置默认状态为pending
  8. self.status = PENDING
  9. // resolve的值
  10. self.value = null
  11. // reject的值
  12. self.reason = null
  13. function resolve(value) {
  14. if (self.status === PENDING) {
  15. // 保存resolve的值
  16. self.value = value
  17. // 切换状态 resolved
  18. self.status = FULFILLED
  19. }
  20. }
  21. function reject(value) {
  22. if (self.status === PENDING) {
  23. // 保存reject的值
  24. self.reason = value
  25. // 切换状态 rejected
  26. self.status = REJECTED
  27. }
  28. }
  29. // 把 resolve 和 reject 能力赋予执行器
  30. try{
  31. execute(resolve, reject)
  32. }catch(e){
  33. reject(e)
  34. }
  35. }

2. then方法的行为

每一个 promise 实例一定有个 then 方法,由此我们不难想到,then 方法应该装在 Promise 构造函数的原型对象上

  1. Promise1.prototype.then = function (onFulfilled, onRejected) {
  2. let self = this
  3. // 注意,onResolved 和 onRejected必须是函数;如果不是,我们此处用一个透传来兜底
  4. onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : value => value
  5. onRejected = typeof onRejected === 'function' ? onRejected : reason => { throw reason };
  6. if (self.status === FULFILLED) {
  7. // 若是fulfilled状态,则执行fulfilled对应方法
  8. onFulfilled(self.value)
  9. } else if (self.status === REJECTED) {
  10. // 若是 rejected 状态,则执行 rejected 对应方法
  11. onRejected(self.reason);
  12. }
  13. }

3. 运行

  1. const PENDING = 'pending';
  2. const FULFILLED = 'fulfilled';
  3. const REJECTED = 'rejected';
  4. function Promise1(execute) {
  5. // 保存this
  6. let self = this
  7. // status记录当前状态,设置默认状态为pending
  8. self.status = PENDING
  9. // resolve的值
  10. self.value = null
  11. // reject的值
  12. self.reason = null
  13. function resolve(value) {
  14. if (self.status === PENDING) {
  15. // 保存resolve的值
  16. self.value = value
  17. // 切换状态 resolved
  18. self.status = FULFILLED
  19. }
  20. }
  21. function reject(value) {
  22. if (self.status === PENDING) {
  23. // 保存reject的值
  24. self.reason = value
  25. // 切换状态 rejected
  26. self.status = REJECTED
  27. }
  28. }
  29. // 把 resolve 和 reject 能力赋予执行器
  30. try{
  31. execute(resolve, reject)
  32. }catch(e){
  33. reject(e)
  34. }
  35. }
  36. Promise1.prototype.then = function (onFulfilled, onRejected) {
  37. let self = this
  38. // 注意,onResolved 和 onRejected必须是函数;如果不是,我们此处用一个透传来兜底
  39. onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : value => value
  40. onRejected = typeof onRejected === 'function' ? onRejected : reason => { throw reason };
  41. if (self.status === FULFILLED) {
  42. // 若是fulfilled状态,则执行fulfilled对应方法
  43. onFulfilled(self.value)
  44. } else if (self.status === REJECTED) {
  45. // 若是 rejected 状态,则执行 rejected 对应方法
  46. onRejected(self.reason);
  47. }
  48. }
  49. new Promise1((resolve, reject) => {
  50. resolve('成功了')
  51. }).then(value => {
  52. console.log('1', value)
  53. }, (reason) => {
  54. console.log('2', reason)
  55. })
  56. new Promise1(function (resolve, reject) {
  57. reject('错了!');
  58. }).then(function (value) {
  59. console.log('3', value);
  60. }, function (reason) {
  61. console.log('4', reason);
  62. });

打印结果
image.png