zoukankan      html  css  js  c++  java
  • async/await的语法和使用

     1. async 函数

        (1)函数的返回值为promise对象
        (2)promise对象的结果由async函数执行的返回值决定

    2. await 表达式

        (1)await右侧的表达式一般为promise对象, 但也可以是其它的值
        (2)如果表达式是promise对象, await返回的是promise成功的值
        (3)如果表达式是其它值, 直接将此值作为await的返回值

    3. 注意:

        (1)await必须写在async函数中, 但async函数中可以没有await
        (2)如果await的promise失败了, 就会抛出异常, 需要通过try...catch来捕获处理
       async function fn1() {
          return 1
        }
        let res = fn1();
        console.log(res) //Promise { 1 }*/
    
        function fn2() {
          return Promise.reject(2)
        }
    
        async function test() {
          /* let result = fn2().then(value => {
              console.log('value',value)
          },
          reason => {
            console.log('reason',reason)
          });*/
          try {
            let result = await fn2();
            console.log('result', result)
          } catch (error) {
            console.log('error', error)
          }
        }
    
        test()
  • 相关阅读:
    php 记录 一些函数语句
    css hack 笔记 for ie8,ie7
    ubuntu 常用 apt更新命令
    JS iframe 跨域
    xdebug配置注意事项
    (matlab)plot画图的颜色线型
    关于iframe中的js跨站
    201202编程笔记
    各种语言中的unix timestamp
    php xml 摘抄
  • 原文地址:https://www.cnblogs.com/BAHG/p/12917872.html
Copyright © 2011-2022 走看看