Home » Interview Question » What is the difference between synchronous and asynchronous in node.js.

What is the difference between synchronous and asynchronous in node.js.

difference between synchronous and asynchronous in node.js

Synchronous Request
Script stops and waits for the result of first piece of code. That means one task has to happen after the other task. If a customer send a request to database, second task is not came in the process until gating the result for first request.

And Result is
getting first query result
getting the second result

In this example, the program will block in the first line. The next line (console.log) will have to wait fro getting query result from server.

Asynchronous Request

A script execute asynchronously when the browser does not wait to execute the first piece of code , and do in process to execute the next piece of code. In other words, the asynchronous script execute in parallel and do not blocking to execute the second piece of code.

And Result is
getting the second result
getting first query result

In this program second line (console.log) will be executed while the first line query is being processed.

 

, , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">