依崇稀

导航

 

<script>
async function randomDelay(id) {
// Delay between 0 and 1000 ms
const delay = Math.random() * 1000;
return new Promise((resolve) => setTimeout(() => {
setTimeout(console.log, 0, `${id} finished`);
resolve();
}, delay));
}

async function foo() {
const t0 = Date.now();

const p0 = randomDelay(0);
const p1 = randomDelay(1);
const p2 = randomDelay(2);
const p3 = randomDelay(3);
const p4 = randomDelay(4);

await p0;
await p1;
await p2;
await p3;
await p4;

setTimeout(console.log, 0, `${Date.now() - t0}ms elapsed`);
}
foo();

// 1 finished
// 4 finished
// 3 finished
// 0 finished
// 2 finished
// 2219ms elapsed
AsyncParallelizationExample03.js

</script>

posted on 2020-12-16 09:33  依崇稀  阅读(200)  评论(0编辑  收藏  举报