JS中的async/await的用法和理解

1、首先需要理解async 和 await的基本含义

async 是一个修饰符,async 定义的函数会默认的返回一个Promise对象resolve的值,因此对async函数可以直接进行then操作,返回的值即为then方法的传入函数

  1. // 0. async基础用法测试

  2. async function fun0() {

  3. console.log(1)

  4. return 1

  5. }

  6. fun0().then( x => { console.log(x) }) // 输出结果 1, 1,

  7. async function funa() {

  8. console.log('a')

  9. return 'a'

  10. }

  11. funa().then( x => { console.log(x) }) // 输出结果a, a,

  12. async function funo() {

  13. console.log({})

  14. return {}

  15. }

  16. funo().then( x => { console.log(x) }) // 输出结果 {} {}

  17. async function funp() {

  18. console.log('Promise')

  19. return new Promise(function(resolve, reject){

  20. resolve('Promise')

  21. })

  22. }

  23. funp().then( x => { console.log(x) }) // 输出promise promise

await 也是一个修饰符,

await 关键字 只能放在 async 函数内部, await关键字的作用 就是获取 Promise中返回的内容, 获取的是Promise函数中resolve或者reject的值
// 如果await 后面并不是一个Promise的返回值,则会按照同步程序返回值处理

  1. // await 关键字 只能放在 async 函数内部, await关键字的作用 就是获取 Promise中返回的内容, 获取的是Promise函数中resolve或者reject的值

  2. // 如果await 后面并不是一个Promise的返回值,则会按照同步程序返回值处理,为undefined

  3. const bbb = function(){ return 'string'}

  4. async function funAsy() {

  5. const a = await 1

  6. const b = await new Promise((resolve, reject)=>{

  7. setTimeout(function(){

  8. resolve('time')

  9. }, 3000)

  10. })

  11. const c = await bbb()

  12. console.log(a, b, c)

  13. }

  14. funAsy() // 运行结果是 3秒钟之后 ,输出 1, time , string,

  1. // 2.如果不使用promise的方法的话

  2. function log2(time) {

  3. setTimeout(function(){

  4. console.log(time)

  5. return 1

  6. }, time)

  7. }

  8. async function fun1() {

  9. const a = await log2(5000)

  10. const b = await log2(10000)

  11. const c = log2(2000)

  12. console.log(a)

  13. console.log(1)

  14. }

  15. fun1()

  16. // 以上运行结果为: 立刻输出undefined 立刻输出1 2秒后输出2000 5秒后输出5000 10秒后输出10000

 

2、那么由此看来async / await的综合用法如下 

 
 
  1. // 1.定义一个或多个普通函数,函数必须返回Promise对象,如果返回其他类型的数据,将按照普通同步程序处理

  2. function log(time) {

  3. return new Promise((resolve, reject)=> {

  4. setTimeout(function(){

  5. console.log(time)

  6. resolve()

  7. }, time)

  8. })

  9. }

  10. async function fun() {

  11. await log(5000)

  12. await log(10000)

  13. log(1000)

  14. console.log(1)

  15. }

  16. fun()

 

 

  1. // 3. async / await的重要应用

  2. const asy = function(x, time) {

  3. return new Promise((resolve, reject) =>{

  4. setTimeout(()=>{

  5. resolve(x)

  6. }, time)

  7. })

  8. }

  9. const add = async function() {

  10. const a = await asy(3, 5000)

  11. console.log(a)

  12. const b = await asy(4, 10000)

  13. console.log(b)

  14. const c = await asy(5, 15000)

  15. console.log(a,b,c)

  16. const d = a + b +c

  17. console.log(d)

  18. }

  19. add();

  20. // 5秒后输出3 又10秒后输出4 又15秒后输出5 然后立刻输出3,4,5,然后输出12

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值