1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
   |  'use strict';
  var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  var Promise = (function () {   function Promise(fn) {     _classCallCheck(this, Promise);
      this.state = 'pending';     this.value = null;     this.deferreds = [];     this.resolve = this.resolve.bind(this);     this.reject = this.reject.bind(this);     fn(this.resolve, this.reject);   }
    _createClass(Promise, [{     key: 'handle',     value: function handle(deferred) {
        if (this.state === 'pending') {         this.deferreds.push(deferred);         return;       }       var cb = this.state === 'fulfilled' ? deferred.onFulfilled : deferred.onRejected,           ret;       if (cb === null) {         cb = this.state === 'fulfilled' ? deferred.resolve : deferred.reject;         cb(this.value);         return;       }       try {         ret = cb(this.value);         deferred.resolve(ret);       } catch (e) {         deferred.reject(e);       }     }   }, {     key: 'resolve',     value: function resolve(newValue) {       if (newValue && (typeof newValue === 'object' || typeof newValue === 'function')) {         var then = newValue.then;         if (typeof then === 'function') {           then.call(newValue, this.resolve, this.reject);           return;         }       }       this.state = 'fulfilled';       this.value = newValue;       this.finale();     }   }, {     key: 'reject',     value: function reject(reason) {       this.state = 'rejected';       this.value = reason;       this.finale();     }   }, {     key: 'finale',     value: function finale() {       setTimeout((function () {         console.log(this);         this.deferreds.forEach((function (deferred) {
            this.handle(deferred);         }).bind(this));       }).bind(this), 0);     }   }, {     key: 'then',     value: function then(onFulfilled, onRejected) {
        return new Promise((function (resolve, reject) {
          this.handle({           onFulfilled: onFulfilled || null,           onRejected: onRejected || null,           resolve: resolve,           reject: reject         });       }).bind(this));     }   }]);
    return Promise; })();
  var a = new Promise(function (res, rej) {   setTimeout(function () {     var a = 1;     var b = 2;     if (1) {       res(a);     } else {       rej(b);     }   }, 2000); });
  var b = new Promise(function (res, rej) {   setTimeout(function () {     var c = 3;     var d = 4;     if (1) {
        res(c);     } else {       rej(d);     }   }, 2000); });
  a.then(function (y) {   return console.log(y); }, function (n) {   return console.log(n); }).then(function () {   return b; }).then(function (y) {   return console.log(y); });
 
  |