promise

time 2m41s

  1. class MyPromise {
  2. constructor() {
  3. }
  4. }
  5. /*换成mypromise*/
  6. // const p1 = new Promise((resolve, reject) => {
  7. const p1 = new MyPromise((resolve, reject) => {
  8. resolve(1);
  9. })
  10. p1.then((res) => {
  11. console.log(res);
  12. }, err => {
  13. console.log(err);
  14. })
  15. /*1*/

第一步

time 5m54s

  1. class MyPromise {
  2. constructor(executor) {
  3. this.state = 'pending';
  4. this.value = undefined;
  5. this.reason = undefined;
  6. let resolve = (value) => {
  7. this.state = 'fullFilled';
  8. this.value = value
  9. }
  10. let reject = (reason) => {
  11. this.state = 'rejected';
  12. this.reason = reason;
  13. }
  14. executor(resolve, reject);
  15. }
  16. then(onFullFilled, onRejected) {
  17. if (this.state === 'fullFilled') {
  18. onFullFilled(this.value);
  19. }
  20. if (this.state === 'rejected') {
  21. onRejected(this.reason);
  22. }
  23. }
  24. }
  25. /*换成mypromise*/
  26. // const p1 = new Promise((resolve, reject) => {
  27. const p1 = new MyPromise((resolve, reject) => {
  28. resolve(1);
  29. })
  30. p1.then((res) => {
  31. console.log(res);
  32. }, err => {
  33. console.log(err);
  34. })
  35. /*1*/

处理异步

time 6m35s

  1. class MyPromise {
  2. constructor(executor) {
  3. this.state = 'pending';
  4. this.value = undefined;
  5. this.reason = undefined;
  6. this.onFullFilledCallbacks = [];
  7. this.onRejectedCallbacks = [];
  8. let resolve = (value) => {
  9. this.state = 'fullFilled';
  10. this.value = value;
  11. // this.onFullFilledCallbacks.forEach(fn => fn(this.value));
  12. this.onFullFilledCallbacks.forEach(fn => fn());
  13. }
  14. let reject = (reason) => {
  15. this.state = 'rejected';
  16. this.reason = reason;
  17. // this.onRejectedCallbacks.forEach(fn => fn(this.value));
  18. this.onRejectedCallbacks.forEach(fn => fn());
  19. }
  20. executor(resolve, reject);
  21. }
  22. then(onFullFilled, onRejected) {
  23. if (this.state === 'fullFilled') {
  24. onFullFilled(this.value);
  25. }
  26. if (this.state === 'rejected') {
  27. onRejected(this.reason);
  28. }
  29. if (this.state === 'pending') {
  30. /* this.onFullFilledCallbacks.push(onFullFilled);
  31. this.onRejectedCallbacks.push(onRejected);*/
  32. this.onFullFilledCallbacks.push(() => {
  33. onFullFilled(this.value);
  34. });
  35. this.onRejectedCallbacks.push(()=>{
  36. onRejected(this.reason);
  37. });
  38. }
  39. }
  40. }
  41. /*换成mypromise*/
  42. // const p1 = new Promise((resolve, reject) => {
  43. const p1 = new MyPromise((resolve, reject) => {
  44. setTimeout(() => {
  45. resolve(1);
  46. }, 1000)
  47. })
  48. p1.then((res) => {
  49. console.log(res);
  50. }, err => {
  51. console.log(err);
  52. })
  53. p1.then((res) => {
  54. console.log(res);
  55. }, err => {
  56. console.log(err);
  57. })
  58. /*1 1*/

处理链式操作

time 10m20s

  1. class MyPromise {
  2. constructor(executor) {
  3. this.state = 'pending';
  4. this.value = undefined;
  5. this.reason = undefined;
  6. this.onFullFilledCallbacks = [];
  7. this.onRejectedCallbacks = [];
  8. let resolve = (value) => {
  9. this.state = 'fullFilled';
  10. this.value = value;
  11. // this.onFullFilledCallbacks.forEach(fn => fn(this.value));
  12. this.onFullFilledCallbacks.forEach(fn => fn());
  13. }
  14. let reject = (reason) => {
  15. this.state = 'rejected';
  16. this.reason = reason;
  17. // this.onRejectedCallbacks.forEach(fn => fn(this.value));
  18. this.onRejectedCallbacks.forEach(fn => fn());
  19. }
  20. executor(resolve, reject);
  21. }
  22. then(onFullFilled, onRejected) {
  23. /*因为判断是同步执行的*/
  24. const p2 = new MyPromise((resolve, reject) => {
  25. let x;
  26. if (this.state === 'fullFilled') {
  27. x = onFullFilled(this.value);
  28. // console.log('165:'+x)
  29. resolve(x);
  30. }
  31. if (this.state === 'rejected') {
  32. x = onRejected(this.reason);
  33. resolve(x);
  34. }
  35. if (this.state === 'pending') {
  36. /* this.onFullFilledCallbacks.push(onFullFilled);
  37. this.onRejectedCallbacks.push(onRejected);*/
  38. this.onFullFilledCallbacks.push(() => {
  39. x= onFullFilled(this.value);
  40. resolve(x);
  41. });
  42. this.onRejectedCallbacks.push(() => {
  43. x= onRejected(this.reason);
  44. resolve(x);
  45. });
  46. }
  47. })
  48. return p2;
  49. }
  50. }
  51. /*换成mypromise*/
  52. // const p1 = new Promise((resolve, reject) => {
  53. const p1 = new MyPromise((resolve, reject) => {
  54. // setTimeout(() => {
  55. // resolve(1);
  56. reject(1);
  57. // }, 1000)
  58. })
  59. const p2 = p1.then((res) => {
  60. // console.log(res);
  61. return res + 1;
  62. }, err => {
  63. // console.log(err)
  64. return err + 2;
  65. })
  66. p2.then(res => {
  67. console.log(res,'success');
  68. }, err => {
  69. console.log(err,'error');
  70. })
  71. /*3 success*/

处理new MyPromise

time 16m35s

time 34m06s