一、基础版
    手写方法时,根据功能来添加代码,不要死记代码

    1. let myPromise = new Promise((resolve, reject)=>{
    2. setTimeout(()=>{resolve('resolved')},1000)
    3. });
    4. myPromise.then((res)=>{},(reason)=>{}).catch((error)=>{});
    5. function MPromise(excutor){
    6. //创建的promise状态为pending
    7. this.PromiseState = 'pending';
    8. this.PromiseResult = null;
    9. //判断resolve和reject接受参数是不是promise对象的方法
    10. this.checkPromise = function(item){
    11. //粗略判断是否有then方法
    12. return typeof item === 'function'
    13. }
    14. //创建给excutor传入的resolve和reject方法
    15. function resolve(param){
    16. let isPromise = param && this.checkPromise(param);
    17. //判断resolve处理的是否为promise,如果是的话,promise的状态由resolve处理的promise决定
    18. if(!isPromise){
    19. console.log('change PromiseState')
    20. this.PromiseState = 'fullfilled'
    21. this.PromiseResult = param
    22. }else{
    23. console.log('isPromise')
    24. //如果是promise,等参数promise状态改变后调用起then方法,
    25. //将resolve和reject方法传进入then回调函数,fullfilled时调用resolve,rejected时调用reject
    26. param.then(resolve,this.reject)
    27. }
    28. }
    29. function reject(param){
    30. this.PromiseState = 'rejected'
    31. this.PromiseResult = param
    32. }
    33. //(1)then方法,可以根据promise状选择性执行传入函数
    34. //(2)根据参数函数执行结果返回值,创建新的promise并返回
    35. //次函数有两个功能,为了解耦,将每个功能写为独立的函数
    36. this.callBackHandler = function (handler){
    37. let currentPromiseState = this.PromiseState,
    38. promiseResult = this.PromiseResult,
    39. handlerResult;
    40. if(currentPromiseState === 'fullfilled'){
    41. handlerResult = handler.onResolved(promiseResult)
    42. }else if(currentPromiseState === 'rejected'){
    43. handlerResult = handler.onrejected(promiseResult)
    44. }
    45. console.log('handlerResult', handlerResult)
    46. return handlerResult
    47. }
    48. this.createNewPromise = function (handlerResult){
    49. //先判断是否为promise,如果不是则创建一个新promise
    50. let isPromise = this.checkPromise(handlerResult);
    51. if(isPromise) return handlerResult
    52. return new MPromise((resolve)=>{resolve(handlerResult)})
    53. }
    54. //this.then = function(cb1,cb2){
    55. // const currentPromiseState = this.PromiseState,
    56. // promiseResult = this.PromiseResult
    57. // if(currentPromiseState === 'fullfilled'){
    58. // cb1(promiseResult)
    59. // }else if(currentPromiseState === 'rejected'){
    60. // cb2(promiseResult)
    61. // }
    62. //}
    63. this.then = function(cb1,cb2){
    64. //先执行回调函数,并保存返回值
    65. let handler = {onResolved : cb1,onrejected : cb2},
    66. promiseResult = this.callBackHandler(handler),
    67. newPromise = this.createNewPromise(promiseResult);
    68. return newPromise
    69. }
    70. this.catch = function(cb){
    71. 判断promiseState是否为rejected,才执行catch的回调函数
    72. //先执行回调函数,并保存返回值
    73. let handler = {onrejected : cb},
    74. promiseResult = this.callBackHandler(handler),
    75. newPromise = this.createNewPromise(promiseResult);
    76. return newPromise
    77. }
    78. //给excutor函数提供两个函数方法参数,然后执行
    79. excutor(resolve.bind(this),reject.bind(this))
    80. }
    81. let myPromise = new MPromise((resolve, reject)=>{
    82. setTimeout(()=>{resolve('resolved')},1000)
    83. });
    84. myPromise
    85. .then((res)=>{console.log(res)},(reason)=>{console.log(reason)})
    86. .catch((error)=>{})