纵有疾风起
人生不言弃

简单实现一个Promise

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
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录