1. function MyPromise(fn){
    2. // 保存this指向
    3. that = this;
    4. // 初始化状态
    5. that.status = "pending";
    6. // value 用来保存最后的结果的
    7. that.value = null;
    8. fn(resolve, reject);
    9. resolve = function(value) {
    10. if(that.status === "pending") {
    11. that.statue = "resolve";
    12. that.value = value;
    13. }
    14. }
    15. reject = function(value) {
    16. if(that.status === "pending"){
    17. that.value = value;
    18. }
    19. }
    20. }
    21. MyPromise.prototype.then = function (fn){
    22. if(this.status === "resolve") {
    23. fn(this.value);
    24. }
    25. }
    26. let p = new MyPromise( (resolve, reject) => {
    27. let num = Math.random()*100;
    28. if(num<100){
    29. resolve(num);
    30. }else {
    31. reject(num);
    32. }
    33. } )
    34. p.then( res => console.log(res));