关于Dart语言async/await特性的若干技术疑问
Great questions—async/await in Dart can feel tricky at first, especially when mixing it with Dart’s single-threaded model. Let’s unpack your two key points clearly:
1. What does await do, and why is the async keyword mandatory?
Let’s start with your first observation: when you run await fetchUserOrder(); inside createOrderMessage, you’re right that the function pauses until the Future returned by fetchUserOrder completes. But here’s the critical detail you’re missing: this pause doesn’t block the entire Dart thread.
Dart uses an event loop to manage asynchronous operations. When your async function hits an await, it:
- Saves its current state (variables, where it left off in code)
- Gives control back to the event loop, allowing other pending tasks (like UI updates, other async callbacks) to run
- Once the
Futurefinishes, the event loop resumes yourasyncfunction from the exact point it paused, with the resolved value of theFuture
Now, why do you need the async keyword? It serves two non-negotiable purposes:
- It tells Dart that this function contains
awaitcalls (you can’t useawaitin a non-asyncfunction) - It automatically wraps the function’s return value in a
Future, even if you return a plain value. For example,createOrderMessagereturns aString, but because it’s markedasync, Dart turns that into aFuture<String>.
Your question about whether createOrderMessage is "essentially a synchronous function" is a common misconception. It’s not synchronous because:
- The function returns immediately with a
Future(the caller doesn’t have to wait for it to finish unless they useawait) - While waiting for the
Futureto complete, the thread is free to do other work. If you removedasyncand tried to make it synchronous, you’d have to block the thread entirely (which is bad practice in Dart, especially for UI apps).
For example, if you called createOrderMessage() without await in main, you’d get a Future<String> instance printed immediately, not the final order message—proof that it’s asynchronous.
2. How do multiple Future calls execute in Dart’s single-threaded model?
Dart is single-threaded, but that doesn’t mean asynchronous operations have to run sequentially. When you call getA(), getB(), getC() without using await right away:
- Each function call runs synchronously first (so any code inside
getAthat’s not wrapped in aFutureexecutes immediately) - The asynchronous part of each function (like the delay in
Future.delayed) is added to Dart’s event queue as separate tasks - These tasks run "concurrently"—the event loop processes them one at a time, but they don’t wait for each other to start. So if all three have a 2-second delay, they’ll all finish around the same time, not 6 seconds total.
Let’s use a concrete example to show this:
Future<String> getA() => Future.delayed(const Duration(seconds: 2), () => "A"); Future<String> getB() => Future.delayed(const Duration(seconds: 2), () => "B"); Future<String> getC() => Future.delayed(const Duration(seconds: 2), () => "C"); Future<void> main() async { print("Starting all tasks..."); final fa = getA(); final fb = getB(); final fc = getC(); print("All tasks have been queued."); // Now we wait for each to finish print(await fa); print(await fb); print(await fc); print("All tasks completed!"); }
This code will output:
Starting all tasks... All tasks have been queued. A B C All tasks completed!
Notice that the 2-second delay only happens once, not three times. That’s because all three Future.delayed tasks started at the same time. If you had written await getA(); await getB(); await getC();, they’d run sequentially (taking 6 seconds total), since each await would pause the function until the previous Future finishes.
To sum it up: calling multiple Future-returning functions without immediate await lets their asynchronous operations run concurrently (in a single-threaded way, via the event loop), not sequentially.
内容的提问来源于stack exchange,提问作者Priyshrm

