码迷,mamicode.com
首页 > Web开发 > 详细

js async03

时间:2020-01-16 12:52:26      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:rom   js async   instant   sync   date   after   div   style   out   

function resolveAfter2Seconds() {
  console.log(‘starting slow promise at:‘+ new Date().getSeconds())
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(‘slow‘)
      console.log(‘slow promise is done at:‘+ new Date().getSeconds())
    }, 2000 )
  })
}
function resolveAfter1Second() {
  console.log(‘starting fast promise at:‘+ new Date().getSeconds())
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(‘fast‘)
      console.log(‘fast promise is done at:‘+ new Date().getSeconds())
    }, 1000 )
  })
}
const concurrentStart = async function(){
  console.log(‘==CONCURRENT START WITH await == at:‘+ new Date().getSeconds())
  const slow = resolveAfter2Seconds()
  const fast = resolveAfter1Second()
  
  // Executon gets here almostinstantly
  console.log(await slow + ‘ at: ‘+new Date().getSeconds())
  console.log(await fast + ‘ at: ‘+new Date().getSeconds())
}
concurrentStart()

//

starting slow promise at:33
starting fast promise at:33
fast promise is done at:34
slow promise is done at:35
slow at: 35
fast at: 35

js async03

标签:rom   js async   instant   sync   date   after   div   style   out   

原文地址:https://www.cnblogs.com/anch/p/12200416.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!