js async03
Posted anch
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js async03相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章
Swift新async/await并发中利用Task防止指定代码片段执行的数据竞争(Data Race)问题
MongoDB 使用 Async.js 与 $in 保持顺序 [重复]