首页 > Web开发 > 详细

js async06

时间:2020-01-16 13:21:21      阅读:82      评论:0      收藏:0      [点我收藏+]

 1 function resolveAfter2Seconds() {
 2   console.log(‘slow start at: ‘ + new Date().getSeconds())
 3   return new Promise(resolve => {
 4     setTimeout(() => {
 5       resolve(‘slow‘)
 6       console.log(‘slow done at: ‘ + new Date().getSeconds())
 7     }, 2000 )
 8   })
 9 }
10 function resolveAfter1Second() {
11   console.log(‘fast start at: ‘ + new Date().getSeconds())
12   return new Promise(resolve => {
13     setTimeout(() => {
14       resolve(‘fast‘)
15       console.log(‘fast done at: ‘ + new Date().getSeconds())
16     }, 1000 )
17   })
18 }
19 var parallelPromise = function() {
20   console.log(‘==PARALLEL WITH Promise.then== at: ‘ + new Date().getSeconds())
21   resolveAfter2Seconds().then(message => console.log(message + " at: " + new Date().getSeconds()))
22   resolveAfter1Second().then(message => console.log(message + " at: " + new Date().getSeconds()))
23 }
24 
25 parallelPromise()

//

==PARALLEL WITH Promise.then== at: 49
slow start at: 49
fast start at: 49
fast done at: 50
fast at: 50
slow done at: 51
slow at: 51

js async06

原文:https://www.cnblogs.com/anch/p/12200437.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!