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

js async05

时间:2020-01-16 12:58:43      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:rom   cond   promise   gets   function   cti   fast   done   console   

function resolveAfter2Seconds() {
  console.log(‘slow start at: ‘ + new Date().getSeconds())
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(‘slow‘)
      console.log(‘slow done at: ‘ + new Date().getSeconds())
    }, 2000 )
  })
}
function resolveAfter1Second() {
  console.log(‘fast start at: ‘ + new Date().getSeconds())
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(‘fast‘)
      console.log(‘fast done at: ‘ + new Date().getSeconds())
    }, 1000 )
  })
}
var parallel = async function() {
  console.log(‘==PARALLEL WITH await Promise.all== at: ‘ + new Date().getSeconds())
  await Promise.all([
    (async () => console.log(await resolveAfter2Seconds() + ‘ at: ‘ + new Date().getSeconds()))(),
    (async () => console.log(await resolveAfter1Second() + ‘ at: ‘ + new Date().getSeconds()))()
  ])
  console.log(‘parallel done at: ‘ + new Date().getSeconds())
}
parallel()

//

==PARALLEL WITH await Promise.all== at: 50
slow start at: 50
fast start at: 50
fast done at: 51
fast at: 51
slow done at: 52
slow at: 52
parallel done at: 52

js async05

标签:rom   cond   promise   gets   function   cti   fast   done   console   

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

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