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()
posted @ 2020-05-19 16:40  BAHG  阅读(634)  评论(0编辑  收藏  举报