async/await
continuations
asynchronous programming
programming patterns
software development

What's the relationship between the async/await pattern and continuations?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

The relationship between the async/await pattern and continuations is intrinsic to how asynchronous programming has evolved. Both of these concepts are foundational to managing asynchronous tasks within a program, particularly in languages like C# and JavaScript. Understanding their interaction clarifies why async/await has become a preferred pattern in modern programming.

Asynchronous Programming: The Basics

Asynchronous programming is crucial for improving the performance of applications by handling multiple operations concurrently, especially I/O-bound tasks. It enables applications to perform other tasks while waiting for an operation, such as a network request, to complete.

Continuations

In programming, a "continuation" is an abstraction that represents the state of a program at a particular point and defines how the program should proceed from there. When dealing with asynchronous tasks, a continuation specifies the next steps to execute once an asynchronous operation completes.

Consider the classical approach before async/await was introduced:

  • Async: A method is marked with the `async` keyword, which allows for the usage of `await` within the method. It signals that the method contains asynchronous operations.
  • Await: The `await` keyword is placed before a call to an asynchronous operation. It instructs the program to asynchronously wait for the operation to complete before proceeding.
  • The compiler transforms the method into a state machine behind the scenes.
  • A continuation is automatically created, which resumes the execution of the method once the awaited task completes.
  • The complexity of manually handling continuations is abstracted away, offering a cleaner syntax and reducing errors associated with manual continuation management.
  • Dividing the method into parts—before, during, and after the await call.
  • Creating a continuation that's executed upon the task's completion.
  • Managing exception flows and ensuring that resources are properly disposed of.
  • Overhead from task scheduling and state machine management.
  • Potentially excessive memory allocations in certain high-frequency asynchronous patterns.
  • UI Programming: Avoids blocking the main thread in applications with graphical interfaces, thereby ensuring responsiveness.
  • I/O-Bound Tasks: Efficiently handles operations such as file and network I/O without blocking threads.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions