Quantcast
Channel: What is the difference between synchronous and asynchronous programming (in node.js) - Stack Overflow
Viewing all articles
Browse latest Browse all 11

Answer by Willem van der Veen for What is the difference between synchronous and asynchronous programming (in node.js)

$
0
0

Asynchronous programming in JS:

Synchronous

  • Stops execution of further code until this is done.
  • Because it this stoppage of further execution, synchronous code is called 'blocking'. Blocking in the sense that no other code will be executed.

Asynchronous

  • Execution of this is deferred to the event loop, this is a construct in a JS virtual machine which executes asynchronous functions (after the stack of synchronous functions is empty).
  • Asynchronous code is called non blocking because it doesn't block further code from running.

Example:

// This function is synchronousfunction log(arg) {    console.log(arg)}log(1);// This function is asynchronoussetTimeout(() => {    console.log(2)}, 0);log(3)
  • The example logs 1, 3, 2.
  • 2 is logged last because it is inside a asynchronous function which is executed after the stack is empty.

Viewing all articles
Browse latest Browse all 11

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>