zoukankan      html  css  js  c++  java
  • JavaScript笔记 #06# Promise简单例子

    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);
    });
  • 相关阅读:
    MyBatis中文文档
    网络编程
    Django
    Django
    Django
    Django
    Django
    Django
    Django
    Django
  • 原文地址:https://www.cnblogs.com/xkxf/p/9593829.html
Copyright © 2011-2022 走看看