Skip to content

Commit

Permalink
example showing that await doesn't block event loop
Browse files Browse the repository at this point in the history
  • Loading branch information
thlorenz committed Feb 16, 2018
1 parent fce51c9 commit 5afbdd7
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions 15-non-blocking.js
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)

0 comments on commit 5afbdd7

Please sign in to comment.