Google News
logo
Dart - Interview Questions
What is the difference between "async" and "sync" functions in Dart?
In Dart, functions can be categorized as either "async" or "sync" based on how they handle asynchronous operations. Here's a comparison between async and sync functions:

1. Execution Model :

   * Sync Functions : Synchronous functions execute sequentially, blocking the execution of other code until they complete. They do not involve any asynchronous operations and are suitable for performing tasks that do not require waiting for external resources or I/O operations.
   
   * Async Functions : Asynchronous functions execute asynchronously and do not block the execution of other code. They can perform time-consuming operations, such as I/O operations or waiting for external resources, without halting the program's progress. Async functions use non-blocking mechanisms, such as Futures or Streams, to handle asynchronous tasks.


2. Syntax :

   * Sync Functions : Sync functions do not have any special keywords associated with them. They are the default type of function in Dart. You define a sync function by specifying the return type and function name, followed by the parameter list and function body. The execution flows in a synchronous manner from the beginning to the end of the function.
   
   * Async Functions : Async functions are marked with the "async" keyword in their function declaration. The "async" keyword indicates that the function contains asynchronous code and can use the "await" keyword to pause its execution until asynchronous operations complete. An async function returns a Future that represents the eventual completion of the function.


3. Usage of "await" :

   * Sync Functions : Sync functions cannot use the "await" keyword because they do not involve asynchronous operations. The execution of a sync function proceeds immediately to the next line after each statement.
   
   * Async Functions : Async functions can use the "await" keyword to pause their execution until an asynchronous operation completes. The "await" keyword allows the function to wait for the result of an asynchronous operation before proceeding to the next line of code. The use of "await" is only valid inside an async function.
4. Return Type :

   * Sync Functions : Sync functions can have any valid Dart return type, such as int, String, bool, etc. They explicitly return a value using the "return" statement, or implicitly return "null" if no value is specified.
   
   * Async Functions : Async functions return a Future object that represents the eventual completion of the function. The return type of an async function is either "Future" or "Future<T>", where "T" represents the type of the value that the function eventually returns.

Here's an example to illustrate the difference between async and sync functions:
void main() {
  syncFunction();
  print('After syncFunction');
  asyncFunction();
  print('After asyncFunction');
}

void syncFunction() {
  print('Sync function');
}

Future<void> asyncFunction() async {
  print('Async function started');
  await Future.delayed(Duration(seconds: 2));
  print('Async function completed');
}​

Output :
Sync function
After syncFunction
Async function started
After asyncFunction
Async function completed​

In the example above, the `syncFunction` is a sync function, and it executes synchronously without any delays. The execution proceeds to the next line immediately after the function call.

The `asyncFunction` is an async function. It is marked with the "async" keyword, allowing the use of "await" inside the function. Inside the async function, we use `await` before `Future.delayed` to introduce a delay of 2 seconds. As a result, the execution of the async function pauses at the `await` statement, allowing other code (e.g., the next `print` statement) to execute.
Advertisement