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

js async04

时间:2020-01-16 12:23:27      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:return   promise   messages   gets   time   ESS   mis   ntp   sync   

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 )
  })
}
const concurrentPromise = function() {
  console.log(‘CONSURRENT START WITH promise at: ‘ + new Date().getSeconds())
  return Promise.all([resolveAfter2Seconds(), resolveAfter1Second()]).then(messages => {
    console.log(messages[0] + ‘ at: ‘ + new Date().getSeconds()) 
    console.log(messages[1] + ‘ at: ‘ + new Date().getSeconds())
  })
}
concurrentPromise()

//

CONSURRENT START WITH promise at: 42
slow start at: 42
fast start at: 42
fast done at: 43
slow done at: 44
slow at: 44
fast at: 44

js async04

标签:return   promise   messages   gets   time   ESS   mis   ntp   sync   

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

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