js async06
2021-04-29 13:29
标签:time pre after star color one get rom ons
// ==PARALLEL WITH Promise.then== at: 49 js async06 标签:time pre after star color one get rom ons 原文地址:https://www.cnblogs.com/anch/p/12200437.html 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()
slow start at: 49
fast start at: 49
fast done at: 50
fast at: 50
slow done at: 51
slow at: 51
上一篇:8 个有用的 JS 技巧