Javascript : async 和await的例子

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

  function resolveAfter2Seconds() {
    console.log("starting slow promise")
    return new Promise(resolve => {
      setTimeout(function() {
        resolve("slow")
        console.log("slow promise is done")
      }, 2000)
    })
  }
  
  function resolveAfter1Second() {
    console.log("starting fast promise")
    return new Promise(resolve => {
      setTimeout(function() {
        resolve("fast")
        console.log("fast promise is done")
      }, 1000)
    })
  }
  
  async function sequentialStart() {
    console.log('==SEQUENTIAL START==')
    var d  = new Date();
    var n  = d.getTime();
    // 1. Execution gets here almost instantly
    const slow = await resolveAfter2Seconds()
    console.log(slow) // 2. this runs 2 seconds after 1.
  
    const fast = await resolveAfter1Second()
    console.log(fast) // 3. this runs 3 seconds after 1.
    var dd   = new Date();
    var nn   = dd.getTime();
    console.log(`sequentialStart : ${nn- n} 毫秒!`);
  }
  
  async function concurrentStart() {
    
    console.log('==CONCURRENT START with await==');
    var d  = new Date();
    var n  = d.getTime();
    const slow = resolveAfter2Seconds(); // starts timer immediately
    const fast = resolveAfter1Second(); // starts timer immediately
  
    // 1. Execution gets here almost instantly
    console.log(await slow) // 2. this runs 2 seconds after 1.
    console.log(await fast) // 3. this runs 2 seconds after 1., immediately after 2., since fast is already resolved
    var dd   = new Date();
    var nn   = dd.getTime();
    console.log(`concurrentStart : ${nn- n} 毫秒!`);
  }
  
  function concurrentPromise() {
    console.log('==CONCURRENT START with Promise.all==')
    return Promise.all([resolveAfter2Seconds(), resolveAfter1Second()]).then((messages) => {
      console.log(messages[0]); // slow
      console.log(messages[1]); // fast
    })
  }
  
  async function parallel() {
    console.log('==PARALLEL with await Promise.all==')
    var d  = new Date();
    var n  = d.getTime();
    // Start 2 "jobs" in parallel and wait for both of them to complete
    await Promise.all([
        (async()=>console.log(await resolveAfter2Seconds()))(),
        (async()=>console.log(await resolveAfter1Second()))()
    ])
    var dd   = new Date();
    var nn   = dd.getTime();
    console.log(`parallel : ${nn- n} 毫秒!`);
  }
  
  sequentialStart();
  concurrentStart();
  parallel();

  

输出:

==SEQUENTIAL START==
starting slow promise
==CONCURRENT START with await==
starting slow promise
starting fast promise
==PARALLEL with await Promise.all==
starting slow promise
starting fast promise
fast promise is done
fast promise is done
fast
slow promise is done
slow
starting fast promise
slow promise is done
slow
fast
concurrentStart : 2015 毫秒!
slow promise is done
slow
parallel : 2017 毫秒!
fast promise is done
fast
sequentialStart : 3026 毫秒!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值