手写简易promise

    科技2022-07-13  124

    //resolve和reject function Promise(fn) { this.status = "pending" this.value = "" const that = this try { fn(resolve, reject) } catch (e) { reject(e) } function resolve(value) { that.value = value; that.status = "resloved" } function reject(value) { that.value = value; that.status = "rejected" } } //then方法实现 Promise.prototype.then = function (onReslved, onRejected) { if (self.status === 'resolved') { onReslved(this.value); } if (self.status === 'rejected') { onRejected(this.reason); } }
    Processed: 0.013, SQL: 8