repl.it
@tnobody/

non-blocking-vs-blocking.js

Nodejs

Showing that you cannot turn async, non-blocking functions into pseudo blocking functions

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
async function wait() {
  return {then: r => setTimeout(r, 1)}
}

async function waitSync() {
  let block = true;
  while(block) {
    wait().then(() => block = false)
  }

  // we'll never reach this code
  console.log('waited 2 seconds async');
}

console.log('Before wait');
wait().then(() => console.log('after 1 ms'));
console.log('After wait')

// If comment out this section you'll see that the code will not continue
/* 
console.log('Before waitSync');
waitSync();

// we'll never reach this code
console.log('After waitSync')
 */
node v10.16.0