步骤

1、 搭框架实现同步改变内部状态的三种情况 resolve(1) reject(2) throw ‘Error’ 只改变一次状态

  1. function Promise(excetor){
  2. let _this = this;
  3. _this.PromiseStatus = "pedding",
  4. _this.PromiseResult = null,
  5. _this.callbacks = [];
  6. function resolve(value){
  7. if(_this.PromiseStatus!=='pedding')return
  8. _this.PromiseStatus = 'fulfilled'
  9. _this.PromiseResult = value;
  10. }
  11. function reject(reason){
  12. if(_this.PromiseStatus!=='pedding')return
  13. _this.PromiseStatus = 'rejected'
  14. _this.PromiseResult = reason;
  15. }
  16. try{
  17. excetor(resolve,reject)
  18. }catch(error){
  19. reject(error)
  20. throw 'promise 内部错误' + error
  21. }
  22. }

2、 实现内部只能改变一次状态 .then resolveFun rejectFun 加入异步执行

_

  1. function Promise(excetor){
  2. let _this = this,
  3. _this.PromiseStatus = "pedding",
  4. _this.PromiseResult = null,
  5. _this.callbacks = [];
  6. function resolve(value){
  7. if(_this.PromiseStatus!=='pedding')return
  8. _this.PromiseStatus = 'fulfilled'
  9. _this.PromiseResult = value;
  10. setTimeout(()=>{
  11. _this.callbacks.forEach(item=>{
  12. item.resolveFun()
  13. })
  14. })
  15. }
  16. function reject(reason){
  17. if(_this.PromiseStatus!=='pedding')return
  18. _this.PromiseStatus = 'rejected'
  19. _this.PromiseResult = reason;
  20. setTimeout(()=>{
  21. _this.callbacks.forEach(item=>{
  22. item.rejectFun()
  23. })
  24. })
  25. }
  26. try{
  27. excetor(resolve,reject)
  28. }catch(error){
  29. reject(error)
  30. throw 'promise 内部错误' + error
  31. }
  32. }
  33. Promise.prototype.then(resolveFun,rejectFun){
  34. if(_this.PromiseStatus === 'fulfilled'){
  35. resolveFun(this.PromiseResult)
  36. }
  37. if(_this.PromiseStatus === 'rejected'){
  38. rejectFun(this.PromiseResult)
  39. }
  40. if(_this.PromiseStatus === 'pedding'){
  41. this.callbacks.push({
  42. resolveFun,
  43. rejectFun
  44. })
  45. }
  46. }

3、_对then方法返回结果进行处理(是否返回promise) 返回一个promise 对then里面的promise处理状态改变, 对状态改变添加异步, 多次then的处理 优化_resolveFun, rejectFun缺省的情况

_

  1. Promise.prototype.then = function(resolveFun,rejectFun){
  2. let _this = this;
  3. if(typeof resolveFun!=='function')resolveFun = value=>value;
  4. if(typeof rejectFun!=='function') rejectFun = reason => {throw reason};
  5. return new Promise((resolve,reject)=>{
  6. function dealStatus(type){
  7. try{
  8. let result = type(_this.PromiseResult)
  9. if(result instanceof Promise){
  10. result.then(v=>{
  11. resolve(v)
  12. },r=>{
  13. reject(r)
  14. })
  15. }else{
  16. resolve(result)
  17. }
  18. }catch(error){
  19. reject(error)
  20. }
  21. }
  22. if(_this.PromiseStatus === 'fulfilled'){
  23. setTimeout(()=>{
  24. dealStatus(resolveFun)
  25. })
  26. }
  27. if(_this.PromiseStatus === 'rejected'){
  28. setTimeout(()=>{
  29. dealStatus(rejectFun)
  30. })
  31. }
  32. if(_this.PromiseStatus === 'pedding'){
  33. this.callbacks.push({
  34. resolveFun:function(){
  35. dealStatus(resolveFun)
  36. },
  37. rejectFun:function(){
  38. dealStatus(rejectFun)
  39. }
  40. })
  41. }
  42. })
  43. }

_

4、添加catch this.then(undefined,rejectFun)

  1. Promise.prototype.catch=function(rejectFun){
  2. return this.then(undefined,rejectFun)
  3. }

9、添加Promise.resolve

  1. Promise.resolve = function(value){
  2. return new Promise((resolve,reject) => {
  3. if(value instanceOf Promise){
  4. value.then(v=>{
  5. resolve(v)
  6. },r=>{
  7. reject(r)
  8. })
  9. }else{
  10. resolve(value)
  11. }
  12. })
  13. }

10、添加Promise.reject

  1. Promise.reject = function(reason){
  2. return new Promise((resolve,reject) => {
  3. reject(reason)
  4. })
  5. }

11、添加Promise.all

原理: 数组中多个Promise对象 , 依次执行,全部执行成功后,resolve, 否则reject

  1. Promise.all = function(promises){
  2. return new Promise((resolve,reject)=>{
  3. let count, arr=[];
  4. for(let i=0;i<promises.length;i++){
  5. promises[i].then(v=>{
  6. count++
  7. arr[i] = v
  8. if(count === promises.length){
  9. resolve(arr)
  10. }
  11. },r=>{
  12. reject(r)
  13. })
  14. }
  15. })
  16. }

12、添加Promise.race

原理:返回一个promise实例。 在多个promise对象在循环处理中,根据最先得到的结果判断promise内部的状态,结果值为promise的值

  1. Promise.race = function(promises){
  2. return new Promise((resolve,reject)=>{
  3. for(let i=0;i<promises.length;i++){
  4. promises[i].then(v=>{
  5. resolve(v)
  6. },r=>{
  7. reject(r)
  8. })
  9. }
  10. })
  11. }

13、完整版

  1. function Promise(excetor){
  2. let _this = this;
  3. _this.PromiseStatus = "pedding",
  4. _this.PromiseResult = null,
  5. _this.callbacks = [];
  6. function resolve(value){
  7. if(_this.PromiseStatus!=='pedding')return
  8. _this.PromiseStatus = 'fulfilled'
  9. _this.PromiseResult = value;
  10. setTimeout(()=>{
  11. _this.callbacks.forEach(item=>{
  12. item.resolveFun()
  13. })
  14. })
  15. }
  16. function reject(reason){
  17. if(_this.PromiseStatus!=='pedding')return
  18. _this.PromiseStatus = 'rejected'
  19. _this.PromiseResult = reason;
  20. setTimeout(()=>{
  21. _this.callbacks.forEach(item=>{
  22. item.rejectFun()
  23. })
  24. })
  25. }
  26. try{
  27. excetor(resolve,reject)
  28. }catch(error){
  29. reject(error)
  30. throw 'promise 内部错误' + error
  31. }
  32. }
  33. Promise.prototype.then = function(resolveFun,rejectFun){
  34. let _this = this;
  35. if(typeof resolveFun!=='function')resolveFun = value=>value;
  36. if(typeof rejectFun!=='function') rejectFun = reason => { throw reason };
  37. return new Promise((resolve,reject)=>{
  38. function dealStatus(type){
  39. try{
  40. let result = type(_this.PromiseResult)
  41. if(result instanceof Promise){
  42. result.then(v=>{
  43. resolve(v)
  44. },r=>{
  45. reject(r)
  46. })
  47. }else{
  48. resolve(result)
  49. }
  50. }catch(error){
  51. reject(error)
  52. }
  53. }
  54. if(_this.PromiseStatus === 'fulfilled'){
  55. setTimeout(()=>{
  56. dealStatus(resolveFun)
  57. })
  58. }
  59. if(_this.PromiseStatus === 'rejected'){
  60. setTimeout(()=>{
  61. dealStatus(rejectFun)
  62. })
  63. }
  64. if(_this.PromiseStatus === 'pedding'){
  65. this.callbacks.push({
  66. resolveFun:function(){
  67. dealStatus(resolveFun)
  68. },
  69. rejectFun:function(){
  70. dealStatus(rejectFun)
  71. }
  72. })
  73. }
  74. })
  75. }
  76. Promise.prototype.catch=function(rejectFun){
  77. return this.then(undefined,rejectFun)
  78. }
  79. Promise.resolve = function(value){
  80. return new Promise((resolve,reject) => {
  81. if(value instanceof Promise){
  82. value.then(v=>{
  83. resolve(v)
  84. },r=>{
  85. reject(r)
  86. })
  87. }else{
  88. resolve(value)
  89. }
  90. })
  91. }
  92. Promise.reject = function(reason){
  93. return new Promise((resolve,reject) => {
  94. reject(reason)
  95. })
  96. }
  97. Promise.all = function(promises){
  98. let count, arr=[];
  99. return new Promise((resolve,reject)=>{
  100. for(let i=0;i<promises.length;i++){
  101. promises[i].then(v=>{
  102. count++
  103. arr[i] = v
  104. if(count === promises.length){
  105. resolve(arr)
  106. }
  107. },r=>{
  108. reject(r)
  109. })
  110. }
  111. })
  112. }
  113. Promise.race = function(promises){
  114. return new Promise((resolve,reject)=>{
  115. for(let i=0;i<promises.length;i++){
  116. promises[i].then(v=>{
  117. resolve(v)
  118. },r=>{
  119. reject(r)
  120. })
  121. }
  122. })
  123. }
  124. // 不管promise最后的状态,在执行完then或catch指定的回调函数以后,都会执行finally方法指定的回调函数。
  125. Promise.finally = function(fn) {
  126. return this.then((data) => {
  127. return Promise.resole(fn()).then(() => value)
  128. }, (err) => {
  129. return Promise.resole(fn()).then(() => { throw err })
  130. })
  131. }

14、class

  1. export default class MPromise {
  2. constructor(excetor) {
  3. console.log('111111////')
  4. let _this = this;
  5. (_this.PromiseStatus = "pedding"),
  6. (_this.PromiseResult = null),
  7. (_this.callbacks = []);
  8. function resolve(value) {
  9. if (_this.PromiseStatus !== "pedding") return;
  10. _this.PromiseStatus = "fulfilled";
  11. _this.PromiseResult = value;
  12. setTimeout(() => {
  13. _this.callbacks.forEach((item) => {
  14. item.resolveFun();
  15. });
  16. });
  17. }
  18. function reject(reason) {
  19. if (_this.PromiseStatus !== "pedding") return;
  20. _this.PromiseStatus = "rejected";
  21. _this.PromiseResult = reason;
  22. setTimeout(() => {
  23. _this.callbacks.forEach((item) => {
  24. item.rejectFun();
  25. });
  26. });
  27. }
  28. try {
  29. excetor(resolve, reject);
  30. } catch (error) {
  31. reject(error);
  32. throw "promise 内部错误" + error;
  33. }
  34. }
  35. then(resolveFun, rejectFun) {
  36. let _this = this;
  37. if (typeof resolveFun !== "function") resolveFun = (value) => value;
  38. if (typeof rejectFun !== "function")
  39. rejectFun = (reason) => {
  40. throw reason;
  41. };
  42. return new Promise((resolve, reject) => {
  43. function dealStatus(type) {
  44. try {
  45. let result = type(_this.PromiseResult);
  46. if (result instanceof Promise) {
  47. result.then(
  48. (v) => {
  49. resolve(v);
  50. },
  51. (r) => {
  52. reject(r);
  53. }
  54. );
  55. } else {
  56. resolve(result);
  57. }
  58. } catch (error) {
  59. reject(error);
  60. }
  61. }
  62. if (_this.PromiseStatus === "fulfilled") {
  63. setTimeout(() => {
  64. dealStatus(resolveFun);
  65. });
  66. }
  67. if (_this.PromiseStatus === "rejected") {
  68. setTimeout(() => {
  69. dealStatus(rejectFun);
  70. });
  71. }
  72. if (_this.PromiseStatus === "pedding") {
  73. this.callbacks.push({
  74. resolveFun: function () {
  75. dealStatus(resolveFun);
  76. },
  77. rejectFun: function () {
  78. dealStatus(rejectFun);
  79. },
  80. });
  81. }
  82. });
  83. }
  84. catch(rejectFun) {
  85. return this.then(undefined, rejectFun);
  86. }
  87. static resolve(value) {
  88. return new Promise((resolve, reject) => {
  89. if (value instanceof Promise) {
  90. value.then(
  91. (v) => {
  92. resolve(v);
  93. },
  94. (r) => {
  95. reject(r);
  96. }
  97. );
  98. } else {
  99. resolve(value);
  100. }
  101. });
  102. }
  103. static reject(reason) {
  104. return new Promise((resolve, reject) => {
  105. reject(reason);
  106. });
  107. }
  108. static all(promises) {
  109. let count,
  110. arr = [];
  111. return new Promise((resolve, reject) => {
  112. for (let i = 0; i < promises.length; i++) {
  113. promises[i].then(
  114. (v) => {
  115. count++;
  116. arr[i] = v;
  117. if (count === promises.length) {
  118. resolve(arr);
  119. }
  120. },
  121. (r) => {
  122. reject(r);
  123. }
  124. );
  125. }
  126. });
  127. }
  128. static race(promises) {
  129. return new Promise((resolve, reject) => {
  130. for (let i = 0; i < promises.length; i++) {
  131. promises[i].then(
  132. (v) => {
  133. resolve(v);
  134. },
  135. (r) => {
  136. reject(r);
  137. }
  138. );
  139. }
  140. });
  141. }
  142. static finally(fn) {
  143. return this.then(
  144. (value) => Promise.resolve(fn()).then((v) => {
  145. console.log(v,value)
  146. value
  147. }),
  148. (error) =>
  149. Promise.resolve(fn()).then((err) => {
  150. throw error;
  151. })
  152. );
  153. }
  154. }