zoukankan      html  css  js  c++  java
  • 12.20 async关键字的学习

    1.async 修饰的函数返回结果是一个promise实例对象

    • 1.函数无返回值
        //1.无返回值(返回一个状态成功值为undefined的promsie实例对象)
        async function fn1() {}
        let result1 = fn1()
        console.log(result1); // Promise {<fulfilled>: undefined}
    
    • 2.函数有返回值
        //2.1 返回非promise值(返回一个状态成功值为返回值的promsie实例对象)
        async function fn2() {
          return 'hello'
        }
        let result2 = fn2()
        console.log(result2); // Promise {<fulfilled>: "hello"}
    
        //2.2.1 返回成功的promise(返回一个状态成功、值为返回的promsie的成功值的promsie实例对象)
        async function fn3() {
          return Promise.resolve('success')
        }
        let result3 = fn3()
        console.log(result3); // Promise {<fulfilled>: "success"}
    
        //2.2.2 返回失败的promise(返回一个状态失败、值为返回的promsie的失败值的promsie实例对象)
        async function fn4() {
          return Promise.reject('failed')
        }
        let result4 = fn4()
        console.log(result4); // Promise {<rejected>: "failed"}
    
    • 3.函数抛出异常
        //3.抛出异常(返回一个状态失败、值为异常值的promsie实例对象)
        async function fn5() {
          // throw new Error('error')
          throw 'error'
        }
        let result5 = fn5()
        console.log(result5); // Promise {<rejected>: "error"}
    
  • 相关阅读:
    Python记录12:迭代器+生成器+生成式
    Python记录11:叠加多个装饰器+有参装饰器
    Python记录10:模块
    Day7
    Day7
    Day7
    Day7
    Day7
    Day7
    Day7
  • 原文地址:https://www.cnblogs.com/xjt31/p/14164345.html
Copyright © 2011-2022 走看看