DEV Community

Emin Vergil
Emin Vergil

Posted on • Updated on

Async vs sync in programming

Definition

Async and sync are two terms that are commonly used in the world of programming, and they refer to the way in which a program or piece of code executes.

Async, short for "asynchronous," refers to code that is executed independently of the main program flow. This means that an async operation can run in the background without blocking the execution of the rest of the program. This is useful in situations where a long-running operation needs to be performed, such as making an API request or accessing a large database.

On the other hand, sync, short for "synchronous," refers to code that is executed in the same order in which it is written. This means that each line of code must be executed in sequence, and the next line of code will not be executed until the current line has completed. This can cause a program to "freeze" or become unresponsive if a long-running operation is performed synchronously.

Differences

One of the key differences between async and sync is the way in which they handle concurrency, or the ability of a program to execute multiple operations at the same time. In an async program, multiple operations can be performed concurrently, allowing the program to make efficient use of available resources. In a sync program, only one operation can be performed at a time, which can result in slower performance.

Another difference is the way in which async and sync operations are handled by the programmer. In an async program, the programmer needs to use special techniques, such as callbacks or promises, to ensure that the program flows correctly. In a sync program, the programmer can simply write the code in the order in which it should be executed.

In conclusion, async and sync are two important concepts in the world of programming, and they refer to the way in which a program or piece of code executes. Async code is executed independently of the main program flow, while sync code is executed in the same order in which it is written. Each approach has its own advantages and disadvantages, and the right choice will depend on the specific needs of the program.

Example

Here is an example of async and sync code in JavaScript.

First, here is an example of sync code:

// This is a sync function that calculates the sum of two numbers
function add(a, b) {
  // The calculation is performed synchronously
  // The next line will not be executed until this one is complete
  return a + b;
}

// This is a sync function that calculates the product of two numbers
function multiply(a, b) {
  // The calculation is performed synchronously
  // The next line will not be executed until this one is complete
  return a * b;
}

// This is a sync function that uses the add and multiply functions
function calculate(a, b) {
  // The add and multiply functions are called synchronously
  // Each line of code is executed in the order in which it is written
  const sum = add(a, b);
  const product = multiply(a, b);
  return sum * product;
}

// This code calls the calculate function synchronously
// The program will "freeze" until the calculation is complete
const result = calculate(2, 3);
console.log(result); // Output: 30

Enter fullscreen mode Exit fullscreen mode

Here is the same code written using async/await:

// This is an async function that calculates the sum of two numbers
async function add(a, b) {
  // The calculation is performed asynchronously
  // The next line will be executed immediately, and the result will be returned later
  return a + b;
}

// This is an async function that calculates the product of two numbers
async function multiply(a, b) {
  // The calculation is performed asynchronously
  // The next line will be executed immediately, and the result will be returned later
  return a * b;
}

// This is an async function that uses the add and multiply functions
async function calculate(a, b) {
  // The add and multiply functions are called asynchronously
  // The await keyword is used to "wait" for the results of the async functions
  const sum = await add(a, b);
  const product = await multiply(a, b);
  return sum * product;
}

// This code calls the calculate function asynchronously
// The program will continue to run while the calculation is performed in the background
calculate(2, 3).then(result => {
  console.log(result); // Output: 30
});

Enter fullscreen mode Exit fullscreen mode

In the async version of the code, the add and multiply functions are marked with the async keyword, indicating that they are async functions. The calculate function also uses the async keyword, and it calls the add and multiply functions using the await keyword. This allows the program to run asynchronously, without blocking the execution of the rest of the code.

Top comments (0)