Javascript Synchronous vs Asynchronous Calls
In my first article, we will compare the javascript synchronous and asynchronous calls with real-time examples. Understanding this concept will help you with coding functions that need to be called as per the business use case.
In JavaScript, a synchronous call is a blocking call that stops or waits for the called code to finish executing before moving on to the next line of code.
Here is an example of a synchronous function call in JavaScript:
function add(a, b) {
return a + b;
}
let result = add(1, 2);
console.log(result); // 3
In this example, the add function is called synchronously, and the program will wait for the add function to finish executing before moving on to the next line of code, which is the console.log statement.
There are many examples of synchronous calls in real-time JavaScript programs. Here are a few examples:
const fs = require("fs");
// Read a file synchronously
let data = fs.readFileSync("data.txt", "utf8");
console.log(data);
// Write to a file synchronously
fs.writeFileSync("output.txt", "Hello World");
const request = require("request-sync");
// Make an HTTP GET request synchronously
let response = request("https://bigdataers.com");
console.log(response.body);
const mysql = require("mysql");
// Connect to a MySQL database
let connection = mysql.createConnection({
host: "localhost",
user: "user",
password: "password",
database: "my_db"
});
// Query the database synchronously
let results = connection.querySync("SELECT * FROM users");
console.log(results);
It is important to note that, in general, it is best to avoid using synchronous calls in JavaScript whenever possible, especially in web applications, because they can block the main thread and make the program unresponsive. Asynchronous calls are generally preferred because they allow the program to continue running while the asynchronous task is being performed in the background.
On the other hand, asynchronous calls in JavaScript do not block the program and allow it to continue executing while the called code is running in the background. We will see about asynchronous function calls in the next section.
In JavaScript, asynchronous calls are used to perform tasks in the background without blocking the main execution of the program. This is useful for tasks that may take a long time to complete, such as making network requests or reading from a file.
There are several ways to make asynchronous calls in JavaScript, including using async/await, promises, and callbacks.
Here is an example of an asynchronous function using the async/await syntax:
async function getData() {
try {
const response = await fetch("https://bigdataers.com");
const data = await response.json();
console.log(data);
} catch (error) {
console.error(error);
}
}
getData();
In this example, the getData function is asynchronous and makes a network request to fetch some JSON data. The await keyword is used to wait for the response and data to be returned before logging the data to the console.
Async/await is a syntax that makes it easier to work with promises and async functions in JavaScript. It allows you to write asynchronous code in a synchronous-looking style, making it easier to read and understand.
Asynchronous calls are useful for tasks that may take a long time to complete, such as making network requests or reading from a file, because they allow the program to continue running while the asynchronous task is being performed in the background.
In short, synchronous function calls are used when there is a need for sequential execution, the input of one function call is dependent on another function call, and also there is no time constraint. Asynchronous function calls are used when the logic can be run independently and in parallel, when there is a time constraint, and when performance needs to be improved.