首页 > 编程语言 > 详细

JavaScript笔记 #06# Promise简单例子

时间:2018-09-05 23:12:35      阅读:227      评论:0      收藏:0      [点我收藏+]

Notes

参考资料:

Promise

JavaScript Promise:简介

 

你去书店借书,按照异步的套路,剧情如下↓

你:“老板,有xxx吗?”

老板:“你等下,我得找一找看,到时候打电话给你。”

然后你就去做其它事情了。

 

1、回调版本:

// 辅助函数
const randomBoolean = () => {
    return Math.random() < 0.5;
};

// 客户对象,处理找书的结果
const customer = {
    dealWithResult: function(success) {
        if(success) {
            console.log(customer:去书店取书);
        } else {
            console.log(customer:有空去别的书店问问);
        }
    }
}

// 书店老板对象,提供一个异步的找书方法。
const bossOfBookstore = {
    askForBook: function(bookName, phoneNumber) {
        setTimeout((phoneNumber) => {
            let result = randomBoolean();
            console.log(bossOfBookstore: + (result ? 找到书了 : 没有这本书));
            customer.dealWithResult(result);
        }, 3000);
    }
}

//debugger;
bossOfBookstore.askForBook(七龙珠, 15298000122);
// → bossOfBookstore:没有这本书
// → customer:有空去别的书店问问 

 

2、Promise版本1:

// 辅助函数
const randomBoolean = () => {
    return Math.random() < 0.5;
};

// 客户对象,处理找书的结果
const customer = {
    dealWithResult: function(success) {
        if(success) {
            console.log(‘customer:去书店取书‘);
        } else {
            console.log(‘customer:有空去别的书店问问‘);
        }
    }
}

// 书店老板对象,提供一个异步的找书方法。
const bossOfBookstore = {
    askForBook: function(bookName, phoneNumber) {
        return new Promise(resolve => {
            setTimeout(phoneNumber => {
                let result = randomBoolean();
                console.log(‘bossOfBookstore:‘ + (result ? ‘找到书了‘ : ‘没有这本书‘));
                resolve(result); // 书店老板才不关心你怎么处理的!    
            }, 3000);            
        });
    }
}

bossOfBookstore.askForBook(‘某某书‘, 15298000122).then(result => {
    customer.dealWithResult(result);
});

 

3、Promise版本2:

// 辅助函数
const randomBoolean = () => {
    return Math.random() < 0.5;
};

// 客户对象,处理找书的结果
const customer = {
    dealWithResult: function(success) {
        if(success) {
            console.log(‘customer:去书店取书‘);
        } else {
            console.log(‘customer:有空去别的书店问问‘);
        }
    }
}

// 书店老板对象,提供一个异步的找书方法。
const bossOfBookstore = {
    askForBook: function(bookName, phoneNumber) {
        return new Promise((resolve, reject) => {
            setTimeout(phoneNumber => {
                let result = randomBoolean();
                console.log(‘bossOfBookstore:‘ + (result ? ‘找到书了‘ : ‘没有这本书‘));
                if (result) {
                    resolve(true);
                } else {
                    reject(false);
                }
            }, 500);            
        });
    }
}

// 写法一
bossOfBookstore.askForBook(‘某某书‘, 15298000122).then(result => {
    customer.dealWithResult(result);
}).catch(result => { 
    customer.dealWithResult(result);
});

// 写法二
bossOfBookstore.askForBook(‘某某书‘, 15298000122).then(result => {
    customer.dealWithResult(result);
}, result => { 
    customer.dealWithResult(result);
});

 

JavaScript笔记 #06# Promise简单例子

原文:https://www.cnblogs.com/xkxf/p/9593829.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!