Back to series
· 13 min read

Synchronous and Asynchronous


I. Basic Concepts of Synchronous and Asynchronous

Synchronous and asynchronous describe how tasks coordinate execution and whether the caller needs to wait for an operation to complete.

1. Synchronous

Synchronous execution is a mode in which there is a direct waiting relationship between making a call and obtaining the result.

After the caller initiates an operation, it must wait for the operation to complete before continuing with subsequent logic that depends on the result.

Typical characteristics:

  • Calls have a clear sequential order.
  • The caller usually needs to wait for the result.
  • Subsequent logic depends on the completion of the current operation.
  • The execution flow is easy to understand and debug.
  • If no other work can be done during the wait, thread resources may be wasted.

Example:

import time

def fetch_data():
    time.sleep(2)
    return {"name": "Alice"}

result = fetch_data()

print(result)
print("继续执行")

When fetch_data() is executed, the current thread waits for about two seconds and continues with subsequent statements only after the function returns.

2. Asynchronous

Asynchronous execution is a mode that decouples initiating an operation from completing it.

After the caller initiates an operation, it does not have to wait continuously for the operation to complete and can continue performing other tasks. Once the operation completes, the result is obtained through a callback, event, Future, Promise, or other mechanism.

Typical characteristics:

  • Initiating an operation and completing it can be separated.
  • The caller does not need to remain blocked waiting.
  • After the task completes, coordination happens through notifications or result objects.
  • Suitable for handling a large number of I/O wait tasks.
  • Requires managing task state, exceptions, and result propagation.

Example:

function fetchData() {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve({ name: "Alice" });
        }, 2000);
    });
}

async function main() {
    const result = await fetchData();
    console.log(result);
}

main();
console.log("主流程继续执行");

After fetchData() returns a Promise, the caller can first execute other logic. await pauses execution of the remaining part of the current async function, but it usually does not block the JavaScript event loop thread.

3. Core Differences Between Synchronous and Asynchronous

Comparison Dimension Synchronous Asynchronous
Call relationship Calls are tightly coupled with result retrieval Calls and result retrieval can be separated
Waiting method Usually waits for the operation to complete Can perform other tasks first
Execution flow Sequential flow Event- or task-driven
Result retrieval Direct return or wait for return Callbacks, Futures, Promises, etc.
Thread usage May block the current thread Can avoid occupying an execution thread during the wait
Programming complexity Usually lower State and error handling are more complex
Applicable scenarios Simple flows, computational tasks I/O-intensive, high-concurrency tasks

Note: Synchronous does not mean single-threaded, and asynchronous does not mean multi-threaded. Synchronous and asynchronous describe coordination and waiting semantics, while single-threaded and multi-threaded describe execution resources and execution entities.


II. Execution Mechanisms of Synchronous and Asynchronous

1. Synchronous Call Mechanism

Synchronous calls usually use direct invocation and return-value passing.

After the caller enters the function, the current call chain must wait for the function to return.

sequenceDiagram
    participant A as Caller
    participant B as Callee

    A->>B: Initiate operation
    Note over A: Wait for result
    B->>B: Execute operation
    B-->>A: Return result
    A->>A: Continue execution

Execution process:

  1. The caller initiates a request.
  2. The callee starts processing.
  3. The caller waits for the processing result.
  4. The callee returns the result.
  5. The caller continues with subsequent logic.

If the callee takes a long time to execute, the caller's execution thread may remain in a waiting state for a long time.

2. Asynchronous Call Mechanism

Asynchronous calls handle task submission and task completion separately.

sequenceDiagram
    participant A as Caller
    participant B as Asynchronous Task
    participant C as Event Notification Mechanism

    A->>B: Submit task
    B-->>A: Return task handle
    A->>A: Perform other work
    B->>B: Process in background or wait for I/O
    B->>C: Task completion notification
    C-->>A: Trigger callback or resume task
    A->>A: Obtain result and continue

Execution process:

  1. The caller submits a task.
  2. The system returns a task handle or an asynchronous result object.
  3. The caller continues with other work.
  4. The asynchronous task progresses within an appropriate execution mechanism.
  5. When the task completes, a notification is sent or the task status is updated.
  6. The caller obtains the result and continues execution.

Asynchronous does not necessarily mean the task executes on a background thread. The task may be completed by mechanisms such as operating system asynchronous I/O, an event loop, a thread pool, or a runtime scheduler.

3. Key Differences Between Synchronous and Asynchronous

Synchronous calls focus on:

  • When does the current operation return?
  • Does the caller need to wait?
  • Does subsequent logic depend on the result of the current call?

Asynchronous calls focus on:

  • How is the task submitted?
  • How does the caller continue execution?
  • How is notification sent after the task completes?
  • How are results and exceptions propagated?

The core difference between the two is not execution speed, but the way tasks coordinate with one another.


III. Synchronous Does Not Mean Blocking, and Asynchronous Does Not Mean Non-blocking

This is one of the most easily confused concepts when understanding concurrency models.

Synchronous/asynchronous and blocking/non-blocking belong to different dimensions.

1. Blocking

Blocking describes the state of the calling thread while it waits for an operation.

After a thread initiates an operation, if the operation has not yet completed and the thread cannot continue executing the current task, it may enter a blocking wait.

For example:

import socket

sock = socket.socket()
sock.connect(("example.com", 80))

data = sock.recv(1024)

If there is currently no data to read, a blocking recv() may cause the calling thread to wait for data to arrive.

2. Non-blocking

Non-blocking describes whether an operation immediately returns control when its conditions are not yet met.

For example:

import socket

sock = socket.socket()
sock.setblocking(False)

try:
    data = sock.recv(1024)
except BlockingIOError:
    print("当前没有可读取的数据")

If the data is not yet ready, a non-blocking call usually returns immediately instead of making the thread keep waiting.

3. Four Combinations

Synchronous/asynchronous and blocking/non-blocking can be combined.

Type Call behavior Typical behavior
Synchronous blocking Waits for the operation to complete Blocking file read
Synchronous non-blocking Returns status immediately; the caller actively checks Non-blocking socket polling
Asynchronous blocking After an asynchronous task is submitted, the caller still waits synchronously for the result Calls join() to wait after submitting an asynchronous task
Asynchronous non-blocking Continues execution after submitting the task and receives a notification upon completion Event loop, asynchronous I/O

Among these, asynchronous blocking is not contradictory. For example, a task may be executed asynchronously by a thread pool, but if the caller subsequently calls Future.result() to wait for the result, the caller still blocks.

4. Illustration of the Four Models

flowchart TB
    A["Operation call model"] --> B["Synchronous"]
    A --> C["Asynchronous"]

    B --> D["Blocking"]
    B --> E["Non-blocking"]

    C --> F["Waiting for asynchronous result"]
    C --> G["Asynchronous non-blocking"]

    D --> H["Thread waits while the operation is incomplete"]
    E --> I["Returns status immediately; actively checks"]
    F --> J["Task executes asynchronously; caller blocks waiting"]
    G --> K["Notifies via event or callback when the task completes"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style H fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style I fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style J fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style K fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

Note that the synchronous/asynchronous nature of an actual API should be judged in combination with how results are delivered and how the caller behaves; it cannot be judged solely by the function name or by whether it returns immediately.


IV. Core Mechanisms of Asynchronous Programming

Asynchronous programming is not simply about putting a function in the background to execute; it requires a set of mechanisms for task management, state progression, and result delivery.

Common core abstractions include:

  1. Callback.
  2. Future.
  3. Promise.
  4. Event Loop.
  5. Coroutine.
  6. Asynchronous I/O.

1. Callback

A callback is a function that is invoked once a task completes.

function fetchData(callback) {
    setTimeout(() => {
        callback(null, { name: "Alice" });
    }, 1000);
}

fetchData((error, result) => {
    if (error) {
        console.error(error);
        return;
    }

    console.log(result);
});

The caller hands the logic for handling the result to the callback, and once the task completes, the asynchronous execution mechanism triggers the callback.

Advantages:

  • Straightforward to implement.
  • No blocking wait required.
  • Suited to simple event notifications.

Drawbacks:

  • Multiple levels of nesting make the code structure complex.
  • Error propagation tends to be scattered.
  • Dependencies between multiple tasks are hard to manage.
  • Cancellation and timeout control are relatively complicated.

2. Future

A Future represents the result of an asynchronous computation that is not yet complete or has already completed.

It typically has the following states:

  • Pending: the task has not yet completed.
  • Completed: the task completed successfully.
  • Failed: the task failed during execution.
  • Cancelled: the task was cancelled.

Future APIs in different languages do not define cancellation capability, completion states and scheduling strategy in exactly the same way.

Python example:

from concurrent.futures import ThreadPoolExecutor

def calculate():
    return 100

with ThreadPoolExecutor() as executor:
    future = executor.submit(calculate)

    print(future.done())
    result = future.result()

    print(result)

submit() submits the task and returns a Future.

When result() retrieves the result, if the task has not yet completed, the calling thread will wait. Therefore, using a Future does not mean the calling process is inherently non-blocking.

3. Promise

A Promise is the object in JavaScript used to represent the eventual result of an asynchronous operation.

A Promise has three states:

State Meaning
Pending Awaiting completion
Fulfilled Operation succeeded
Rejected Operation failed

A Promise's state can only change from Pending to Fulfilled or Rejected, and cannot change again once settled.

const promise = new Promise((resolve, reject) => {
    const success = true;

    if (success) {
        resolve("执行成功");
    } else {
        reject(new Error("执行失败"));
    }
});

promise
    .then(result => {
        console.log(result);
    })
    .catch(error => {
        console.error(error);
    });

Promises solve part of the problem of traditional callback nesting and error propagation.

They can organize multiple asynchronous operations through chained calls:

fetch("/api/user")
    .then(response => response.json())
    .then(user => fetch(`/api/orders/${user.id}`))
    .then(response => response.json())
    .then(orders => console.log(orders))
    .catch(error => console.error(error));

4. async / await

async and await are a syntactic abstraction built on top of asynchronous result mechanisms such as Promises.

They give asynchronous code a reading structure close to that of synchronous code.

async function getUser() {
    try {
        const response = await fetch("/api/user");

        if (!response.ok) {
            throw new Error("请求失败");
        }

        return await response.json();
    } catch (error) {
        console.error(error);
        throw error;
    }
}

Key semantics:

  • async functions always return a Promise.
  • await waits for the result of a Promise or thenable.
  • await pauses the subsequent execution of the current async function.
  • await typically does not block the JavaScript main thread.
  • The code after await resumes execution once the asynchronous result completes.

Pay special attention to:

async function example() {
    const a = await taskA();
    const b = await taskB();

    return [a, b];
}

Here, taskB() does not start until taskA() has completed.

If the two tasks do not depend on each other, you can use:

async function example() {
    const [a, b] = await Promise.all([
        taskA(),
        taskB()
    ]);

    return [a, b];
}

Promise.all() can wait for multiple Promises concurrently, but it does not guarantee that these tasks actually execute in parallel across multiple CPU cores.


V. The Event Loop

The event loop is an important mechanism through which many asynchronous runtimes implement task scheduling.

Both the JavaScript browser environment and Node.js use the event loop model, but the specific phases and scheduling rules differ.

1. Components of the Event Loop

Taking browser JavaScript as an example, the main components include:

  • Call Stack: executes the current synchronous code.
  • Web APIs: capabilities provided by the browser, such as timers and network requests.
  • Task Queue: holds tasks waiting to be executed.
  • Microtask Queue: holds microtasks such as Promise callbacks.
  • Event Loop: coordinates the call stack and the task queues.

2. Event Loop Execution Flow

flowchart TD
    A["Execute synchronous code"] --> B["Is the call stack empty?"]
    B --> C{"Are there pending microtasks?"}
    C -->|Yes| D["Execute microtasks"]
    D --> C
    C -->|No| E["Browser can render"]
    E --> F["Take one macrotask"]
    F --> G["Execute the macrotask"]
    G --> B

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style C fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style E fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

This is a simplified illustration of the browser event loop. Browsers also involve more complex scheduling rules, such as rendering opportunities, user interaction tasks, timers, and network events.

3. Macrotasks and Microtasks

In the browser environment, common task categories are as follows:

Type Common Sources Characteristics
Macrotask setTimeout, user events, some I/O callbacks Scheduled via the task queue
Microtask Promise.then, queueMicrotask, MutationObserver Usually executed in a batch after the current task ends

Example:

console.log("1");

setTimeout(() => {
    console.log("2");
}, 0);

Promise.resolve().then(() => {
    console.log("3");
});

console.log("4");

Output:

1
4
3
2

Reason:

  1. Synchronous code executes first.
  2. After synchronous code finishes executing, the microtask queue is cleared.
  3. Next, timer tasks in the macrotask queue are executed.

Note: setTimeout(fn, 0) does not mean the function executes immediately; rather, it means it executes as soon as possible after the scheduling conditions are met.

4. Common Pitfalls in Microtasks

New microtasks generated during microtask execution usually continue to execute at the current microtask checkpoint.

If microtasks are continuously generated, it may cause:

  • Macrotasks to be delayed indefinitely.
  • Browser rendering to be delayed.
  • The page to stutter.
  • Event responses to be delayed.

Therefore, you should not infinitely recursively enqueue new microtasks within microtasks.


VI. Implementation Principles of Asynchronous I/O

Asynchronous I/O is an important foundation for high-concurrency network services.

1. Synchronous I/O Model

In traditional blocking I/O, after a thread initiates a read operation, if the data is not yet ready, the thread may wait indefinitely.

sequenceDiagram
    participant A as Application Thread
    participant K as Kernel
    participant D as I/O Device

    A->>K: Initiate read request
    K->>D: Wait for device data
    Note over A: Calling thread blocks and waits
    D-->>K: Data preparation complete
    K-->>A: Return read result
    Note over A: Continue execution

This approach is easy to understand, but when many connections are waiting at the same time, it consumes a relatively large amount of thread resources.

2. Non-blocking I/O

Non-blocking I/O returns immediately when the data is not yet ready.

The application can repeatedly attempt to read, but frequent polling wastes CPU time.

flowchart TD
    A["Initiate non-blocking read"] --> B["Check whether data is ready"]
    B --> C{"Is data ready?"}
    C -->|No| D["Return not ready immediately"]
    D --> E["Check again later"]
    E --> B
    C -->|Yes| F["Read data"]
    F --> G["Process result"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style C fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style E fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

3. I/O Multiplexing

I/O multiplexing allows a single thread to monitor the I/O readiness status of multiple file descriptors.

Common mechanisms:

Mechanism Platform Features
select Multiple platforms Traditional interface, with limitations such as the number of descriptors
poll Multiple platforms Uses an array to manage descriptors
epoll Linux Efficiently manages a large number of connections
kqueue BSD, macOS Event notification mechanism
IOCP Windows Completion notification model

Taking Linux epoll as an example:

  1. The application registers the file descriptors to be monitored.
  2. The kernel maintains the set of events of interest.
  3. When the state of a file descriptor changes, the kernel records a readiness event.
  4. The application calls epoll_wait() to obtain readiness events.
  5. The application handles the corresponding I/O.
flowchart TD
    A["Create epoll instance"] --> B["Register file descriptor"]
    B --> C["Kernel monitors I/O events"]
    C --> D["Wait for ready events"]
    D --> E{"Are there any ready events"}
    E -->|No| D
    E -->|Yes| F["Return ready descriptors"]
    F --> G["Application handles I/O"]
    G --> D

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

It is particularly important to distinguish:

  • I/O multiplexing usually reports whether file descriptors are ready.
  • The actual data reading is still performed by the application.
  • epoll_wait() itself may block waiting for events.
  • Using I/O multiplexing does not automatically mean the entire I/O operation is completed asynchronously.

4. True Asynchronous I/O

True asynchronous I/O usually has the operating system or runtime drive the I/O operation and notify the application after completion.

The application does not have to keep waiting for data preparation and the read process after initiating the operation.

Different operating systems provide different implementations, for example:

  • Linux io_uring.
  • Windows IOCP.
  • Related asynchronous I/O interfaces on platforms such as macOS and BSD.

However, different interfaces differ in completion semantics, buffer requirements, and scope of applicability.

5. Differences Between I/O Multiplexing and Asynchronous I/O

Comparison Aspect I/O Multiplexing Asynchronous I/O
Core mechanism Monitor the readiness state of multiple I/O operations Submit I/O and wait for completion notification
Kernel responsibility Notify which operations can proceed Drive and complete the specific I/O operation
Data reading Usually performed by the application Completed or assisted by the asynchronous I/O mechanism
Waiting method Wait for readiness events Wait for completion events
Programming model Event-driven Completion-driven
Typical interfaces select, poll, epoll io_uring, IOCP

This distinction is very important: high-performance network frameworks often use I/O multiplexing to build asynchronous programming interfaces, but their underlying implementation does not necessarily use true asynchronous I/O in the operating system sense.


7. Asynchronous Programming and Coroutines

A coroutine is a program execution unit that can be paused and resumed.

It allows a task to voluntarily suspend when it encounters a waiting condition, hand control to other tasks, and later resume from the suspension point.

1. Core Characteristics of Coroutines

  • Has an independent execution state.
  • Can suspend at specified points.
  • Can resume execution later.
  • Is usually managed by the language runtime or scheduler.
  • Does not necessarily correspond to an independent operating system thread.

A coroutine is an execution abstraction; asynchrony is a way of coordinating tasks. The two are related but are not the same concept.

2. async / await and Coroutines

Python example:

import asyncio

async def fetch_data():
    print("开始请求")
    await asyncio.sleep(2)
    print("请求完成")
    return {"name": "Alice"}

async def main():
    result = await fetch_data()
    print(result)

asyncio.run(main())

Here:

  • async def defines a coroutine function.
  • Calling a coroutine function returns a coroutine object.
  • await can wait for awaitable objects.
  • The asyncio event loop is responsible for scheduling coroutines.
  • asyncio.sleep() suspends the current coroutine during the wait instead of blocking the entire event loop thread.

3. Concurrent Execution of Multiple Coroutines

import asyncio

async def task(name):
    print(f"{name} 开始")
    await asyncio.sleep(2)
    print(f"{name} 完成")

async def main():
    await asyncio.gather(
        task("任务A"),
        task("任务B"),
        task("任务C")
    )

asyncio.run(main())

The three tasks can make progress alternately within a single event loop thread.

If they mainly perform I/O waiting, the waiting periods can overlap, thereby improving overall throughput.

However, if a coroutine executes a large amount of CPU-intensive code that does not yield control, the event loop may be blocked.

4. Differences Between Coroutines, Threads, and Processes

Comparison Dimension Coroutine Thread Process
Execution abstraction Lightweight task Execution flow Independent running instance
Scheduler Runtime or scheduler Operating system or runtime Operating system manages its threads
Switching cost Usually lower Usually higher Usually even higher
Address space Shares its execution environment Shares its process Usually independent
Parallelism capability Depends on runtime and mapping model Can run in parallel on multiple cores Can run in parallel on multiple cores
Typical use High-concurrency I/O Concurrent task execution Isolation and independent computation

5. Coroutines Cannot Automatically Solve CPU-Intensive Problems

For example:

async def cpu_task():
    result = 0

    for i in range(100000000):
        result += i

    return result

This coroutine does not execute any effective asynchronous wait point.

When it runs in the event loop thread, it continuously occupies that thread, preventing other coroutines from executing in a timely manner.

Solutions include:

  • Submit CPU-intensive tasks to a process pool.
  • Use a thread pool to execute tasks suitable for thread handling.
  • Use dedicated compute services.
  • Use runtimes or language mechanisms that support parallel execution.

VIII. Performance Analysis of Synchronous and Asynchronous

Synchronous and asynchronous approaches have no absolute performance superiority or inferiority; judgment must consider task type, waiting time, concurrency scale, and scheduling overhead.

1. CPU-Intensive Tasks

CPU-intensive tasks mainly consume processor computing resources, for example:

  • Image encoding.
  • Video transcoding.
  • Numerical computation.
  • Cryptographic computation.
  • Large-scale data processing.

The main bottleneck for such tasks is CPU computing power.

Asynchronous I/O cannot directly increase CPU computation speed.

If multiple cores need to be utilized, consider multiprocessing, multithreading, or dedicated parallel computing mechanisms.

2. I/O-Intensive Tasks

I/O-intensive tasks mainly wait for external resources, for example:

  • Network requests.
  • Database queries.
  • File reads.
  • Remote service calls.
  • Message queue consumption.

The main bottleneck for such tasks is usually not the CPU, but waiting for I/O to complete.

Asynchronous models allow the executing thread to handle other tasks during waiting periods, improving resource utilization.

3. Time Comparison Between Synchronous and Asynchronous

Suppose there are three mutually independent network requests, each taking about 2 seconds.

Synchronous sequential execution:

flowchart TD
    A["Request A: 2 seconds"] --> B["Request B: 2 seconds"]
    B --> C["Request C: 2 seconds"]
    C --> D["Total time about 6 seconds"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

Asynchronous concurrent execution:

flowchart TD
    A["Send requests concurrently"] --> B["Request A: 2s"]
    A --> C["Request B: 2s"]
    A --> D["Request C: 2s"]
    B --> E["Collect results"]
    C --> E
    D --> E
    E --> F["Total time about 2s"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px