zoukankan      html  css  js  c++  java
  • promis:异步编程

    promise对象用于延迟计算和异步计算:一个promise对象代表着一个还未完成,但预期将来完成的操作

    5640239-82d99e995d355471.png
    Image.png
    5640239-95d0aace0b59287c.png
    Image.png

    打印结果如下:

    <!DOCTYPE html>
    <html>
         <head>
               <meta charset="UTF-8">
               <title></title>
         </head>
         <body>
         <script>
         console.time("Promise");
         //resolve执行成功后调用
         //reject执行失败时调用
         new Promise(function(resolve, reject) {
               setTimeout(function() {
                    resolve("定时器");
               }, 1000 * 2);
         }).then(function(result) {
               console.log(result);
               console.timeEnd("Promise");
         });
    </script>
         </body>
    </html>
    
    5640239-a81a40f8b99dd7d8.png
    Image.png

    必须要加返回值

    5640239-2fbcb2b439b18862.png
    Image.png
    <!DOCTYPE html>
    <html>
           <head>
                     <meta charset="UTF-8">
                     <title></title>
           </head>
           <body>
           </body>
           <script>
                     console.time('Promise');
                     //resolve 执行成功后调用
                     //reject  执行失败时调用
                     new Promise(function(resolve,reject){
                              setTimeout(function(){
                                        resolve("定时器执行完毕");
                              },1000*2);
                     }).then(function(result){
                              console.log("第一个 then 有异步");
                              //对数据做处理
                              return new Promise(function(resolve, reject){
                                        setTimeout(function(){
                                                 resolve({
                                                           "data" : result
                                                 });
                                        },1000 * 2);
                              });
                     }).then(function(data){
                              //接收完整的数据
                              console.log(data);
                              console.timeEnd('Promise');
                     });
           </script>
    </html>
    
  • 相关阅读:
    java基础英语---第二天
    树莓派的版本
    Linux系统下安装.deb文件
    在Raspberry上安装ROS
    树莓派文件权限的转换
    树莓派中Linux的相关命令
    raspberry连接ssh和vnc
    链表的建立及释放
    一些小细节问题
    关于构建二维动态内存(堆)及释放
  • 原文地址:https://www.cnblogs.com/wangting888/p/9702157.html
Copyright © 2011-2022 走看看