乐趣区

promise-练习转载

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title> 活动名称 </title>
    <meta name="viewport" content="width=320, user-scalable=no, initial-scale=1.0, maximum-scale=1.3, minimum-scale=1.0">
    <!-- 启用 360 浏览器的极速模式 (webkit) -->
    <meta name="renderer" content="webkit">
    <!-- 避免 IE 使用兼容模式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- 针对手持设备优化,主要是针对一些老的不识别 viewport 的浏览器,比如黑莓 -->
    <meta name="HandheldFriendly" content="true">
    <!-- 微软的老式浏览器 -->
    <meta name="MobileOptimized" content="320">
    <!-- uc 强制竖屏 -->
    <meta name="screen-orientation" content="portrait">
    <!-- QQ 强制竖屏 -->
    <meta name="x5-orientation" content="portrait">
    <!-- UC 强制全屏 -->
    <meta name="full-screen" content="yes">
    <!-- QQ 强制全屏 -->
    <meta name="x5-fullscreen" content="true">
    <!-- UC 应用模式 -->
    <meta name="browsermode" content="application">
    <!-- QQ 应用模式 -->
    <meta name="x5-page-mode" content="app">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vant@2.0/lib/index.css">
    <!-- 引入组件 -->
    <script src="js/rem.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vant@2.0/lib/vant.min.js"></script>
</head>
<style>

</style>

<body>

</body>
<script>
    // 买笔
    function buy() {console.log("开始买笔");
        var p = new Promise(function(resolve, reject) {setTimeout(function() {console.log("买了笔芯");
                resolve("数学作业");
            }, 1000);
        });
        return p;
    }
    // 写作业
    function work(data) {console.log("开始写作业:" + data);
        var p = new Promise(function(resolve, reject) {setTimeout(function() {console.log("写完作业");
                resolve("作业本");
            }, 1000);
        });
        return p;
    }
    function out(data) {console.log("开始上交:" + data);
        var p = new Promise(function(resolve, reject) {setTimeout(function() {console.log("上交完毕");
                resolve("得分:A");
            }, 1000);
        });
        return p;
    }
    /* 不建议使用这种方式
    buy().then(function(data){return work(data);
    }).then(function(data){return out(data);
    }).then(function(data){console.log(data);
    });*/

    // 推荐这种简化的写法
    buy().then(work).then(out).then(function(data) {console.log(data+"这是全部数据打印出来看看是个啥");
    });
</script>

</html>

退出移动版