zoukankan      html  css  js  c++  java
  • es6 Promise

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>es6  Promise</title>
     6 </head>
     7 <body>
     8     
     9 </body>
    10 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js"></script>
    11 <script type="text/javascript">
    12 
    13 let p = new Promise((reslove,reject)=>{
    14     let aa = 4567890
    15     // 写业务
    16     reslove(aa)
    17     // reject('sdaa')
    18 })
    19 p
    20 .then(res=>{
    21     // 写业务
    22     console.log(res)
    23     return a()
    24 })
    25 .then(res=>{
    26     console.log(res)
    27 })
    28 .catch(err=>{
    29     console.log(err)
    30 })
    31 
    32 
    33 function a(){
    34     return '哈哈啊哈'
    35 }
    36 
    37 
    38 // 多个方法测试
    39 let test1 = new Promise((resolve,reject)=>{
    40     setTimeout(()=>{
    41         resolve([1,2,3,4,5,6])
    42     },200)
    43 })
    44 
    45 
    46 let test2 = new Promise((resolve,reject)=>{
    47     setTimeout(()=>{
    48         // resolve({a:1,b:'ssds'})
    49         reject('错误')
    50     },500)
    51     
    52 })
    53 
    54 Promise.all([test1,test2])
    55 .then(res=>{
    56     console.log(res)
    57 })
    58 .catch(err=>{
    59     console.log(err)
    60 })
    61 
    62 
    63 Promise.race([test1,test2])
    64 .then(res=>{
    65     console.log(res)
    66 })
    67 .catch(err=>{
    68     console.log(err)
    69 })
    70 
    71 
    72 // Promise.all方法,对于多个方法一起调用,多个方法返回的全部是成功的才会在then打印
    73 // Promise.race方法,对于多个方法一块执行,只返回最先执行的一个
    74 
    75 </script>
  • 相关阅读:
    [转载]qemu-kvm安装配置
    Hadoop通过c语言API访问hdfs
    hadoop和hdfs环境搭建
    OpenCV installation for Ubuntu 12.04
    homework-01
    linux命令2
    压缩tar
    anaconda 安装opencv
    anconda安装第三方库
    开源代码
  • 原文地址:https://www.cnblogs.com/chailuG/p/12450164.html
Copyright © 2011-2022 走看看