时间: 2020-09-10|tag:48次围观|0 条评论

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

原著是一个有趣的人,若有侵权,请通知删除

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《简单实现一个Promise
   

还没有人抢沙发呢~