-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example showing that await doesn't block event loop
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict' | ||
|
||
const { task } = require('./util') | ||
|
||
async function getUsers(ids) { | ||
const users = [] | ||
for (const id of ids) { | ||
const user = await task({ result: { name: `bob-${id}`, id } }) | ||
users.push(user) | ||
} | ||
return users | ||
} | ||
|
||
async function getBatchUno() { | ||
console.log('Getting batch uno') | ||
const users = await getUsers([ 1, 2, 3 ]) | ||
console.log('Got batch uno', users) | ||
} | ||
|
||
async function getBatchDos(ids) { | ||
console.log('Getting batch dos') | ||
const users = await getUsers([ 4, 5, 6 ]) | ||
console.log('Got batch dos', users) | ||
} | ||
|
||
async function getBatchTres(ids) { | ||
console.log('Getting batch tres') | ||
const users = await getUsers([ 7, 8, 9 ]) | ||
console.log('Got batch tres', users) | ||
} | ||
|
||
setTimeout(getBatchUno, 5) | ||
setTimeout(getBatchDos, 10) | ||
setTimeout(getBatchTres, 15) |