function myPromise(fn){ this.state = 'Pending' this.value this.resolve = function(){ if(this.state != 'Pending'){ return } this.state = 'fulfilled' this.value = arguments } this.reject = function(){ this.state = 'rejected' this.value = arguments } this.then = this.catch = function(callback1, callback2){ let callback = (this.state == 'fulfilled' ? callback1 : callback2) let result = callback(...this.value) return new myPromise((resolve)=>{ resolve(result) }) } this.finally = function(callback) { callback() } fn(this.resolve.bind(this), this.reject.bind(this)) }
标签:function,callback2,resolve,一个,value,随便,callback,state,promise From: https://www.cnblogs.com/jiangxiaoxi/p/16817173.html