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);
    });
  • 相关阅读:
    封装好的AFN网络请求框架和MBProgress
    iOS定时器的使用
    iOS去除导航栏和tabbar的1px横线
    移动端加解密
    改变字符串中部分字符传的字体大小和颜色
    关于NSLog
    ipad开发:二维码扫描,摄像头旋转角度问题解决办法
    iOS-图文表并茂,手把手教你GCD
    计算富文本的高度
    jsp打印
  • 原文地址:https://www.cnblogs.com/xkxf/p/9593829.html
Copyright © 2011-2022 走看看