class PromiseClone { constructor (process) { this.status = 'pending' this.msg = '' process(this.resolve.bind(this), this.reject.bind(this)) return this } resolve (val) { this.status = 'fulfilled' this.msg = val } reject (err) { this.status = 'rejected' this.msg = err } then (fufilled, reject) { if(this.status === 'fulfilled') { fufilled(this.msg) } if(this.status === 'rejected') { reject(this.msg) } }}//测试代码var mm=new PromiseClone(function(resolve,reject){ resolve('123');});mm.then(function(success){ console.log(success);},function(){ console.log('fail!');});
文章转载于:https://www.jianshu.com/p/678a6d9de856
原著是一个有趣的人,若有侵权,请通知删除
还没有人抢沙发呢~