DEV Community

Discussion on: NodeJS Non-Blocking Processing

Collapse
 
qm3ster profile image
Mihail Malo • Edited

Any await point yields to the eventloop, much like using setImmediate.
Try the following:

const {log} = console
const proc = x => log('processing', x)
const d = async q => {
  // ⚠️ Attention! Achtung! Attenzione! ⚠️
  // We are awaiting a synchronous value (`undefined`, not a `Promise`) just to create a yield point inside the loop!
  while (q.length) await proc(q.pop())
}
log('starting')
const task = d([3,5,7,9,11,13])
log('started')
await task
log('finished')
Enter fullscreen mode Exit fullscreen mode

You will get started printed before most of the processing lines.