ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
လူစည်တဲ့ coffee counter မှာ order မှာတာကို စဉ်းစားကြည့်ပါ။ ပိုက်ဆံပေးပြီးရင် barista က နံပါတ်ပါတဲ့ ticket တစ်ခု ပေးပြီး ဘေးဖယ်ပေးပါလို့ ပြောပါတယ်။ Latte ထွက်လာတဲ့အထိ counter ရှေ့မှာ မလှုပ်ဘဲ ရပ်စောင့်မနေပါဘူး။ ဖုန်းကြည့်လိုက်၊ စားပွဲရှာလိုက် လုပ်နေပြီး နံပါတ်ခေါ်တဲ့အခါမှ ticket ကို ပေးပြီး ဖျော်ရည်ကို လဲယူပါတယ်။
Dart မှာ အဲဒီ ticket ကို Future လို့ ခေါ်ပါတယ်။ File၊ server၊ timer လို နှေးတဲ့အရာနဲ့ ဆက်သွယ်တဲ့ function က Future ကို ချက်ချင်း ပြန်ပေးပြီး တကယ့်အလုပ်က နောက်မှ ပြီးပါတယ်။ await ဆိုတာ "ကျွန်တော့်အလုပ်ကို ခဏရပ်ထားမယ်၊ ticket အဆင်သင့်ဖြစ်ရင် ပြန်ခေါ်ပါ" လို့ ပြောတာပါ။ Function က စောင့်နေတဲ့အချိန်မှာ ကျန်တဲ့ program က ဆက်အလုပ်လုပ်နေလို့ Flutter screen က animation ဆက်ပြေးနေနိုင်သလို server ကလည်း တခြား visitor တွေကို ဆက်ဖြေနေနိုင်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: ticket ကို ဖျော်ရည်လို့ ထင်မိတာ
Dart beginner အားလုံးလိုလို ပထမဆုံး မှားတတ်တဲ့ အမှားကို ကြည့်ပါ။ fetchOrder() ကို async လို့ ကြေညာထားလို့ ခေါ်လိုက်ရင် အဆုံးထိ run ပြီး String ပြန်မပေးပါဘူး။ Future<String> ကို ပြန်ပေးတာပါ။ await မထည့်ဘဲ အဲဒီ value ကို ဖျော်ရည်အစစ်လို သုံးမိရင် နှစ်မျိုးထဲက တစ်မျိုး ဖြစ်ပါတယ်။
String အဖြစ် အသုံးချဖို့ ကြိုးစားရင် compiler က တားပါတယ်။ Future ပေါ်မှာ .length ခေါ်ရင် "The getter 'length' isn't defined for the type 'Future<String>'" ဆိုတဲ့ error ပေါ်ပါတယ်။ ဒါပေမယ့် print ပဲ လုပ်တာဆိုရင် ဘာမှ မတားပါဘူး။ Output က Instance of 'Future<String>' လို့ ပေါ်ပြီး program က ဆက်သွားပါတယ်။ တကယ့် order ကတော့ ခဏနေမှ ရောက်လာပြီး ဘယ်သူမှ နားမထောင်တော့ပါဘူး။
Future<String> fetchOrder() async {
await Future.delayed(Duration(milliseconds: 100));
return 'Large latte';
}
void main() {
final order = fetchOrder(); // forgot await
print('Order: $order'); // Order: Instance of 'Future<String>'
}Dart က Future ကို ဘာကြောင့် ပြန်ပေးရတာလဲ
Function တစ်ခုက မရှိသေးတဲ့ value ကို return လုပ်လို့ မရပါဘူး။ Dart ကလည်း အဲဒီ value ကို စောင့်ဖို့ program တစ်ခုလုံးကို မရပ်ထားပါဘူး။ ဒါကြောင့် function က နေရာယူထားတဲ့ placeholder ကို ချက်ချင်းပြန်ပေးပြီး နောက်မှ ဖြည့်ပေးမယ်လို့ ကတိပေးတာပါ။ Future<String> ဆိုတဲ့ type က အဲဒီကတိကို code ထဲမှာ မြင်သာအောင် ထားပေးလို့ ပထမအမှားကို compiler က ဖမ်းနိုင်တာပါ။
Core concept: Future နဲ့ event loop
Future<T> ဆိုတာ T type ရှိတဲ့ value တစ်ခုကို မရှိသေးချိန်မှာ နေရာယူပေးတဲ့ object ပါ။ တစ်ချိန်ချိန်မှာ state သုံးမျိုးထဲက တစ်မျိုးမှာ ရှိပါတယ်။ အလုပ်ဆက်လုပ်နေဆဲ (uncompleted)၊ value နဲ့ ပြီးသွားပြီ (completed with a value)၊ ဒါမှမဟုတ် error နဲ့ ပြီးသွားပြီ (completed with an error) ပါ။ Uncompleted ကနေ တစ်ကြိမ်ပဲ ထွက်ပြီး နောက်ပြောင်းမသွားတော့လို့ Future တစ်ခုတည်းကို ဖတ်မယ့်သူ အများကြီးဆီ ပေးထားလို့ရပါတယ်။
Dart က isolate တစ်ခုချင်းစီမှာ thread တစ်ခုတည်းပေါ် code ကို run ပြီး event loop က queue နှစ်ခုနဲ့ မောင်းပါတယ်။ Microtask queue ထဲမှာ တခြားအလုပ်တွေမတိုင်ခင် ပြီးရမယ့် အသေးစား အလုပ်တွေ ရှိပါတယ် (ဥပမာ ပြီးသွားတဲ့ Future ရဲ့ နောက်ဆက်တွဲ အပိုင်း)။ Event queue ထဲမှာတော့ timer၊ file နဲ့ network ရလဒ်၊ ထိတာ နှိပ်တာ စတဲ့ အပြင်ကလာတဲ့ အရာတွေ ရှိပါတယ်။ main() ပြီးသွားရင် loop က microtask အားလုံးကို ကုန်အောင်လုပ်၊ event တစ်ခုကို ယူ၊ microtask တွေကို ထပ်ကုန်အောင်လုပ် ဆိုပြီး ထပ်ခါထပ်ခါ လုပ်ပါတယ်။
| နေရာ | ဘာတွေ ထည့်သလဲ | ဥပမာ |
|---|---|---|
| Synchronous code | အခု run နေတဲ့ call stack | main() နဲ့ ပုံမှန် function call တွေ |
| Microtask queue | အရင်ပြီးရမယ့် အသေးစား job | scheduleMicrotask၊ Future.microtask |
| Event queue | အပြင်က လာတဲ့ event | Timer၊ Future.delayed၊ Future(...)၊ I/O |
import 'dart:async';
void main() {
print('1 sync: start');
Future(() => print('5 event: Future()'));
Future.delayed(Duration(milliseconds: 10), () => print('6 event: delayed 10ms'));
scheduleMicrotask(() => print('3 microtask: scheduleMicrotask'));
Future.microtask(() => print('4 microtask: Future.microtask'));
print('2 sync: end of main');
}Code ကို အပေါ်ကနေ အောက်ကို ဖတ်ရင် 5 နဲ့ 6 က ရှေ့ဆုံးလို ထင်ရပေမယ့် print နံပါတ်တွေက တကယ့်အစီအစဉ်ကို ပြပါတယ်။ Sync code အားလုံး အရင်ပြီးမယ်၊ ပြီးမှ microtask နှစ်ခုက အစဉ်လိုက် run မယ်၊ ပြီးမှ event queue ထဲက task တွေ အလှည့်ကျ run ပါတယ်။
async keyword က function ရဲ့ return type ကို Future ဖြစ်စေပြီး ပုံမှန် return value ကို Future ထဲ အလိုလို ထည့်ပေးပါတယ်။ await ကိုတော့ လူတွေ အမြဲလိုလို နားလည်မှားပါတယ်။ ဒါက thread ကို မပိတ်ပါဘူး။ Function က အဲဒီ line မှာ ခဏရပ်၊ control က event loop ဆီ ပြန်သွားလို့ တခြားအလုပ်တွေ run နိုင်ပြီး await လုပ်ထားတဲ့ Future ပြီးတဲ့အခါ function က ရပ်ခဲ့တဲ့ line ကနေ ဆက်ပါတယ်။ ပထမ await မတိုင်ခင်အထိတော့ async function က တခြား function လိုပဲ synchronous ပဲ run ပါတယ်။
Error တွေလည်း value နဲ့ လမ်းတူတူ သွားပါတယ်။ Body ထဲမှာ throw လုပ်ရင် ပြန်ပေးတဲ့ Future က အဲဒီ error နဲ့ ပြီးသွားပြီး await က caller ထဲမှာ ပြန် throw ပေးလို့ ပုံမှန် try/catch/finally က async နယ်နိမိတ်ကို ကျော်ပြီး အလုပ်လုပ်ပါတယ်။ ဘယ်သူမှ await မလုပ်၊ နားမထောင်တဲ့ Future က error ဖြစ်ရင်တော့ တိတ်တိတ်မနေဘဲ unhandled exception အဖြစ် ထွက်လာပြီး program ရပ်သွားပါတယ်။
- Async function ခေါ်ရင် Future ticket ရမယ်
- await က thread မဟုတ်၊ function ကို ရပ်တယ်
- ဒီအတောအတွင်း event loop က တခြားအလုပ် run
- Future ပြီးရင် value ဒါမှမဟုတ် error ပြန်ရ
Future ကို await မလုပ်ခင် မျက်လုံးထဲ ပုံဖော်ကြည့်ပါ။ အဲဒီ line ကို ဖြတ်သွားရင် တခြား code တွေ run နိုင်တာမို့ function ပြန်ဆက်တဲ့အခါ အခြေအနေတွေ နည်းနည်း ပြောင်းနေနိုင်တယ်ဆိုတာ သတိရပါ။
အခြေခံ ပုံစံ
Future<Result> name(Param param) async {
final value = await slowCall(param);
// a throw here completes the Future with an error
return Result(value);
}
Future<void> main() async {
try {
final result = await name(argument);
} on SomeError catch (e) { /* handle */ }
}async function က Future<Result> ကို ကြေညာပြီး ဘယ်နေရာမှာမဆို await နဲ့ ခဏရပ်နိုင်ပါတယ်။ ခေါ်တဲ့သူက await နဲ့ ယူပြီး error ကို try/on/catch နဲ့ ဖမ်းပါတယ်။
Order မှာပြီး စောင့်ကြည့်မယ်
ဒီ program က order တစ်ခု မှာပြီး ticket က Future ဖြစ်တယ်ဆိုတာ ပြပါတယ်။ အဲဒီနောက် စောင့်ပြီး ရလဒ်ယူပါတယ်။ ဆက်ပြီး ဖျော်ရည်နှစ်မျိုးကို အတူတူ မှာပါတယ်။ နောက်ဆုံးမှာ ဖျက်ကွက်တစ်ခု ဘယ်လို ပေါ်လာလဲ ပြပါတယ်။ await ပါတဲ့ line တိုင်းက function ရပ်နိုင်တဲ့နေရာတွေဖြစ်လို့ နံပါတ်တပ်ထားတဲ့ print တွေက သင်ဖတ်တဲ့အတိုင်း အစီအစဉ်ကျ ထွက်ပါတယ်။
Delay တွေက 100 ms ပဲ ရှိလို့ program က မြန်မြန်ပြီးပါတယ်။ Print တိုင်းက function တစ်ခုထဲမှာ await ပြီးမှ ဖြစ်တာမို့ run တိုင်း output တူပါတယ်။
Future<String> fetchOrder(String drink) async {
await Future.delayed(Duration(milliseconds: 100));
if (drink.isEmpty) {
throw ArgumentError('drink must not be empty');
}
return 'Large $drink';
}
Future<void> main() async {
print('1. placing order');
final ticket = fetchOrder('latte');
print('2. ticket is a Future, not a String: ${ticket is Future<String>}');
final order = await ticket;
print('3. received: $order');
final both = await Future.wait([fetchOrder('mocha'), fetchOrder('tea')]);
print('4. both finished: $both');
try {
await fetchOrder('');
} on ArgumentError catch (e) {
print('5. failed: ${e.message}');
} finally {
print('6. counter closed');
}
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- async function and its Future
Future<String> fetchOrder(String drink) async {async ကြောင့် body ထဲမှာ await နဲ့ ရပ်နိုင်ပါတယ်။ Declared type က Future<String> ဖြစ်ပေမယ့် body ထဲက return 'Large $drink' က ပုံမှန် String ပါ။ Dart က Future ထဲ အလိုလို ထည့်ပေးပါတယ်။
- Pausing without blocking
await Future.delayed(Duration(milliseconds: 100));Future.delayed က သတ်မှတ်ချိန်ပြည့်မှ ပြီးပါတယ်။ Function က ဒီမှာ ရပ်၊ event loop က အားနေပြီး program ရဲ့ တခြားအပိုင်း တစ်ခုမှ စောင့်နေစရာ မလိုပါဘူး။
- A throw becomes a failed Future
throw ArgumentError('drink must not be empty');async function ထဲက throw က caller ကို ချက်ချင်း မပျက်စေပါဘူး။ ပြန်ပေးမယ့် Future ကို error နဲ့ ပြီးစေပြီး အဲဒီ Future ကို ဖတ်တဲ့ await တစ်ခုမှာ error ပေါ်လာပါတယ်။
- Holding the Future
final ticket = fetchOrder('latte');ဒီမှာ await မပါလို့ ticket က Future ကိုယ်တိုင်ပါ။ Call က စတင်ပြီးပြီ၊ မပြီးသေးပါဘူး။ ဒါကြောင့် print 2 မှာ ticket က Future<String> ဖြစ်တယ်ဆိုတာ ပြနိုင်တာပါ။
- Resuming with the value
final order = await ticket;ဒီ line မှာ function ရပ်ပြီး ticket ပြီးတဲ့အခါ ပြန်ဆက်ပါတယ်။ အခု order ရဲ့ type က String အစစ်ဖြစ်လို့ String method တွေ သုံးလို့ရပါပြီ။
- Starting calls together
final both = await Future.wait([fetchOrder('mocha'), fetchOrder('tea')]);Call နှစ်ခုစလုံး await မလုပ်ခင် စတင်ပြီးသားမို့ delay တွေ ထပ်နေပါတယ်။ ရလဒ် list က ပြီးတဲ့အစီအစဉ် မဟုတ်ဘဲ ထည့်ပေးခဲ့တဲ့ list ရဲ့ အစီအစဉ်အတိုင်း ရှိပါတယ်။
- Catching the failure
} on ArgumentError catch (e) {await က သိမ်းထားတဲ့ error ကို ဒီနေရာမှာ ပြန် throw ပေးလို့ ပုံမှန် try/on/catch/finally သုံးလို့ရပါတယ်။ finally ကတော့ order အောင်မြင်သည်ဖြစ်စေ၊ မအောင်မြင်သည်ဖြစ်စေ run ပါတယ်။
1. placing order
2. ticket is a Future, not a String: true
3. received: Large latte
4. both finished: [Large mocha, Large tea]
5. failed: drink must not be empty
6. counter closed- Caller က await line ကို ရောက်တယ်
- Function ရပ်တယ်၊ event loop က အားနေတယ်
- Timer ဒါမှမဟုတ် I/O က Future ကို ပြီးစေတယ်
- Function က await နောက်ကနေ ဆက်တယ်
Value နဲ့ ပြီးရင်
The value becomes the result of the await expression.
Error နဲ့ ပြီးရင်
The error is thrown at the await line, where try/catch can see it.
တစ်ခုပြီးမှ တစ်ခု လား၊ အတူတူ လား
await ကို line တစ်ကြောင်းချင်းစီ ရေးရင် ဒုတိယ call က ပထမ call ပြီးမှ စပါတယ်။ ဒုတိယ call က ပထမရဲ့ ရလဒ်ကို လိုတဲ့အခါ ဒါက မှန်ပါတယ်။ ဒါပေမယ့် မှီခိုမှု မရှိရင် အချိန်ကို ဆုံးရှုံးစေပါတယ်။ Call နှစ်ခုလုံးကို အရင်စပြီးမှ အတူတူ await လုပ်ရင် delay တွေ ထပ်နေလို့ စုစုပေါင်းအချိန်က ရှည်ဆုံး call ရဲ့ အချိန်လောက်ပဲ ကြာပါတယ်။
အမျိုးအစားမတူတဲ့ Future နှစ်ခု သုံးခုကို record ပုံစံနဲ့ (a(), b()).wait လို့ ရေးလို့လည်း ရပါတယ်။ ရလဒ်ကို (x, y) လို destructure လုပ်ရင် type တစ်ခုချင်းစီ မပျက်ပါဘူး။ Future.wait ကတော့ list ထဲက type တူတဲ့ Future တွေအတွက် ပိုသင့်တော်ပါတယ်။
Future<int> slowDouble(int n) async {
await Future.delayed(Duration(milliseconds: 100));
return n * 2;
}
Future<void> main() async {
// One after another: the second call starts only after the first finished.
final a = await slowDouble(1);
final b = await slowDouble(2);
print('one by one: $a and $b');
// Together: both calls are already running when we start waiting.
final (x, y) = await (slowDouble(3), slowDouble(4)).wait;
print('together: $x and $y');
}အချိန်ကို တိုင်းကြည့်ပါ
ဒီ code မှာ ပထမအပိုင်းက 200 ms ဝန်းကျင်၊ ဒုတိယအပိုင်းက 100 ms ဝန်းကျင် ကြာပါတယ်။ တိကျတဲ့ အချိန်က စက်ပေါ်မူတည်လို့ Stopwatch နဲ့ ကိုယ်တိုင်တိုင်းကြည့်ပါ၊ output ထဲကိုတော့ အချိန်မထည့်ပါနဲ့။
async function က ချက်ချင်း စပါတယ်
Async function ကို ခေါ်လိုက်တာနဲ့ ပထမ await ရောက်တဲ့အထိ body က caller ရဲ့ call stack ပေါ်မှာပဲ ချက်ချင်း run ပါတယ်။ ပထမ await မှာမှ ရပ်ပြီး Future ကို caller ဆီ ပြန်ပေးပါတယ်။ ဒါကို နားမလည်ရင် print အစီအစဉ်က အံ့ဩစရာ ဖြစ်နေတတ်ပါတယ်။
Future<void> task() async {
print('task: before await');
await null;
print('task: after await');
}
void main() {
print('main: start');
task();
print('main: after calling task');
}Output က main: start၊ task: before await၊ main: after calling task၊ task: after await ဆိုတဲ့ အစီအစဉ်ပါ။ await null လို့ ရေးတာတောင် ပြီးသားတန်ဖိုးကို စောင့်တာ ဖြစ်လို့ task ရဲ့ ကျန်တဲ့အပိုင်းက microtask ဖြစ်သွားပြီး main က ပြီးမှ run ပါတယ်။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
Flutter app မှာ network request၊ database ဖတ်တာ၊ file load တာ အားလုံးက Future ပြန်ပေးပါတယ်။ UI thread က frame တွေ ဆက်ဆွဲနေရလို့ screen code က ရလဒ်ကို await လုပ်ပြီး စောင့်နေစဉ် spinner ပြပါတယ်။ FutureBuilder widget ဟာ ဒီပုံစံအတွက်ပဲ ရှိတာပါ။ Server ဘက်မှာတော့ dart:io နဲ့ ရေးထားတဲ့ Dart service တစ်ခုက request တစ်ခုချင်းစီက အများအားဖြင့် database ဒါမှမဟုတ် တခြား service ကို စောင့်နေရလို့ isolate တစ်ခုတည်းနဲ့ request အများကြီးကို ကိုင်တွယ်နိုင်ပါတယ်။ await လုပ်နေစဉ် loop က နောက် request ကို ဆက်ဖြေပေးလို့ပါ။
Command-line tool တွေမှာလည်း မော်ဒယ်က အတူတူပါပဲ။ File.readAsString()၊ Process.run() နဲ့ HttpClient အားလုံး Future ပြန်ပါတယ်။ တကယ့် code မှာ ticket က တခါတလေ ပျက်တတ်ပါတယ် (timeout၊ မရှိတဲ့ file၊ network မရှိတာ)။ ဒါကြောင့် await လုပ်ပုံက အရေးကြီးပါတယ်။ Message ပြနိုင်တဲ့ဒါမှမဟုတ် ပြန်ကြိုးစားနိုင်တဲ့ layer မှာ try/catch ထားပါ။ Screen တစ်ခုက မှီခိုမှုမရှိတဲ့ resource သုံးခု လိုရင် တစ်ခုပြီးမှ တစ်ခု မ load ဘဲ Future.wait နဲ့ အတူ load ပါ။
အမှားများ
ဖြစ်လေ့ရှိတဲ့ အမှားများ
await မထည့်ဘဲ ခေါ်လိုက်တာ
saveOrder() ကို await မပါဘဲ ခေါ်ရင် အလုပ်က စပါတယ်၊ ဒါပေမယ့် caller က ချက်ချင်း ဆက်သွားပါတယ်။ main ထဲမှာဆိုရင် 'saved' မတိုင်ခင် 'done' ကို print ထုတ်နိုင်ပါတယ်။ saveOrder က နောက်မှ throw လုပ်ရင် ဖမ်းမယ့်သူ မရှိလို့ Dart က 'Unhandled exception: Bad state: boom' လို့ report လုပ်ပြီး process က error နဲ့ ရပ်သွားပါတယ်။ await လုပ်ပါ၊ ဒါမှမဟုတ် fire-and-forget ကို တကယ်ရည်ရွယ်တာဆိုရင် unawaited() နဲ့ ထုပ်ပါ။
forEach ထဲမှာ async callback သုံးတာ
list.forEach((n) async { await save(n); }) က save တစ်ခုချင်းစီကို စောင့်မယ်လို့ ထင်ရပေမယ့် forEach က callback ပြန်ပေးတဲ့ Future တွေကို လျစ်လျူရှုပါတယ်။ နောက်ကလိုင်းက save တစ်ခုမှ မပြီးသေးခင် ချက်ချင်း run ပါတယ်။ တစ်ခုပြီးမှ တစ်ခု လုပ်ချင်ရင် for-in loop ထဲမှာ await သုံးပါ၊ အတူလုပ်ချင်ရင် Future.wait(list.map(save)) သုံးပါ။
မှီခိုမှုမရှိတဲ့ call တွေကို တစ်ခုပြီးမှ တစ်ခု await လုပ်တာ
100 ms ကြာတဲ့ request သုံးခုကို တစ်ခုပြီးမှ တစ်ခု await လုပ်ရင် 300 ms ဝန်းကျင်ကြာပြီး Future.wait နဲ့ဆိုရင် 100 ms ဝန်းကျင်ပဲ ကြာပါတယ်။ နောက် call က အရင် call ရဲ့ ရလဒ်ကို လိုမှသာ တစ်ခုပြီးမှ တစ်ခု လုပ်ပါ။
async ရေးရုံနဲ့ မြန်သွားမယ်လို့ ထင်တာ
async keyword က အလုပ်ကို တခြား thread ဆီ ရွှေ့မပေးပါဘူး။ async function ထဲမှာ loop ကို သန်းနဲ့ချီ ပတ်ရင် အဲဒီအတောအတွင်း event loop တစ်ခုလုံး ပိတ်နေပြီး timer တွေနဲ့ UI က ရပ်သွားပါတယ်။ CPU အလုပ်ကြီးတွေကို isolate နဲ့ run ရပါတယ် (နောက် lesson မှာ ကြည့်ပါမယ်)။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- ဘာမှ မပြန်တဲ့ async function ကို void မဟုတ်ဘဲ Future<void> လို့ ကြေညာပါ။ ဒါမှ caller က await လုပ်နိုင်ပါမယ်။
- Future တစ်ခုကို await လုပ်ပါ၊ handle လုပ်ပါ၊ ဒါမှမဟုတ် unawaited() ထဲ တမင်ထည့်ပါ။ မတော်တဆ ပစ်မထားပါနဲ့။
- မှီခိုမှုမရှိတဲ့ call တွေကို အရင်စပြီးမှ အတူ await လုပ်ပါ။
- Error ကို await တိုင်းမှာ မဟုတ်ဘဲ ပြန်ကြိုးစားတာ၊ message ပြတာလို အဓိပ္ပာယ်ရှိတဲ့ အလုပ်လုပ်နိုင်တဲ့နေရာမှာ ဖမ်းပါ။
Mini challenge
ပစ္စည်းစင်ကို စစ်ကြည့်ပါ
Coffee shop ရဲ့ ပစ္စည်းစင်မှာ item တစ်ခုချင်းစီ ကျန်တဲ့အရေအတွက်ကို async function နဲ့ ရှာပါ။ Item အများကြီးကို အတူစစ်ပြီး limit အောက်ရောက်နေတဲ့ item တွေကို စာရင်းပြုစုပါ။ မရှိတဲ့ item ကို မေးရင် error ဖြစ်ရပါမယ်။
- fetchStock(item) က 50 ms စောင့်ပြီး စင်ပေါ်က အရေအတွက်ကို ပြန်ပေးရမယ်၊ မရှိတဲ့ item ဆိုရင် StateError ကို throw လုပ်ရမယ်
- lowStock(items) က optional named parameter limit (default 5) ယူပြီး lookup အားလုံးကို အတူ စတင်ရမယ်
- limit အောက်က item နာမည်တွေကို ပေးလိုက်တဲ့ အစီအစဉ်အတိုင်း ပြန်ပေးရမယ်
- main() က ရလဒ်ကို print လုပ်ပြီး မရှိတဲ့ item အတွက် ပေါ်လာတဲ့ error message ကို try/catch နဲ့ ဖမ်းပြရမယ်
// Shelf data: item name -> units left.
const shelf = {'beans': 12, 'milk': 2, 'cups': 40, 'lids': 3};
Future<int> fetchStock(String item) async {
// TODO: wait 50 ms with Future.delayed
// TODO: throw StateError('unknown item: ...') when the item is not on the shelf
// TODO: otherwise return the count
}
Future<List<String>> lowStock(List<String> items, {int limit = 5}) async {
// TODO: start every lookup first, then wait for all of them together
// TODO: return the names whose count is below limit, keeping the input order
}
Future<void> main() async {
// TODO: print the low stock for beans, milk, cups and lids
// TODO: call lowStock(['beans', 'sugar']) inside try/catch and print the message
}ရလဒ်: ပထမ line က Low stock: [milk, lids]၊ ဒုတိယ line က Check failed: unknown item: sugar၊ နောက်ဆုံး line က With limit 100: [cups] ဖြစ်ရပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
Future.wait(items.map(fetchStock)) က count list ကို items နဲ့ အစီအစဉ်တူတူ ပြန်ပေးပါတယ်။ index နဲ့ ပြန်တွဲပြီး limit ထက်နည်းတာတွေကို ရွေးပါ။ StateError ရဲ့ message ကို e.message နဲ့ ယူနိုင်ပါတယ်။
အဖြေ ကြည့်ရန်
const shelf = {'beans': 12, 'milk': 2, 'cups': 40, 'lids': 3};
Future<int> fetchStock(String item) async {
await Future.delayed(Duration(milliseconds: 50));
final count = shelf[item];
if (count == null) {
throw StateError('unknown item: $item');
}
return count;
}
Future<List<String>> lowStock(List<String> items, {int limit = 5}) async {
final counts = await Future.wait(items.map(fetchStock));
return [
for (var i = 0; i < items.length; i++)
if (counts[i] < limit) items[i],
];
}
Future<void> main() async {
final low = await lowStock(['beans', 'milk', 'cups', 'lids']);
print('Low stock: $low');
try {
await lowStock(['beans', 'sugar']);
} on StateError catch (e) {
print('Check failed: ${e.message}');
}
final generous = await lowStock(['cups'], limit: 100);
print('With limit 100: $generous');
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Future ဆိုတာ မရှိသေးတဲ့ value ကို နေရာယူတဲ့ object ဖြစ်ပြီး value နဲ့ပဲဖြစ်ဖြစ်၊ error နဲ့ပဲဖြစ်ဖြစ် တစ်ကြိမ်ပဲ ပြီးပါတယ်
- main() ပြီးရင် microtask အားလုံး အရင်၊ ပြီးမှ event queue ထဲက task တစ်ခုချင်းစီ ဆိုတဲ့ အစီအစဉ်နဲ့ ပြေးပါတယ်
- await က function တစ်ခုကိုပဲ ရပ်ပါတယ်၊ thread ကို မပိတ်ပါဘူး၊ error ကိုလည်း အဲဒီ line မှာ ပြန် throw ပေးပါတယ်
- မှီခိုမှုမရှိတဲ့ call တွေကို Future.wait နဲ့ အတူစပါ၊ forEach ထဲက async callback ကို မစောင့်ဘူးဆိုတာ သတိထားပါ
Dart language overview — Dart