異步編程
Dart中通過 Future
和 Stream
實現(xiàn)異步,談?wù)撍麄冎捌垦辏群唵瘟私庖幌率裁唇挟惒健?/p>
Asynchrony, in computer programming, refers to the occurrence of events independent of the main program flow and ways to deal with such events. These may be "outside" events such as the arrival of signals, or actions instigated by a program that take place concurrently with program execution, without the program blocking to wait for results.
-- https://en.wikipedia.org/wiki/Asynchrony_(computer_programming)
異步是獨(dú)立于“主程序流”的一個流充包,類似于子線程,可以在不阻塞主線程的情況下遥椿,執(zhí)行并返回結(jié)果基矮,比如從數(shù)據(jù)庫讀取數(shù)據(jù),通過異步的形式修壕,可以在讀取數(shù)據(jù)時執(zhí)行加載動畫愈捅,等待讀取到數(shù)據(jù)將其顯示到界面上遏考。
Future
Dart 中的 Future
類慈鸠,搭配 await
, async
關(guān)鍵字可以實現(xiàn)簡單的異步。
下面的代碼模擬一個獲取name值的耗時操作:
//模擬耗時操作,獲取到 name 需要 2 秒青团。
Future<String> getName() {
return Future.delayed(Duration(seconds: 2), () => 'Tom');
}
?
//獲取到 name 后將其顯示出來譬巫。
void printName() {
var name = getName();
print(name);
}
?
void main() {
printName();
print('something else');
}
輸出結(jié)果為:
可以看到,main()
里的兩個方法是按代碼順序同步執(zhí)行的督笆,執(zhí)行 printName()
方法時芦昔,getName()
立即返回了一個Future并被打印,然后才打印 “Tom”娃肿。
下面改下代碼:
//模擬耗時操作咕缎,獲取到 name 需要 2 秒。
Future<String> getName() {
return Future.delayed(Duration(seconds: 2), () => 'Tom');
}
?
//獲取到 name 后將其顯示出來料扰。
void printName() async{
var name = await getName();
print(name);
}
?
void main() {
printName();
print('something else');
}
輸出結(jié)果為:
雖然 printName()
的執(zhí)行需要兩秒凭豪,但沒有阻塞其他代碼的執(zhí)行,"something else "先被打印晒杈,兩秒后 "Tom" 被打印出來嫂伞。
改動的部分是 Lines7 的async
和 Lines8 的await
,async
表示程序需要異步執(zhí)行拯钻,await
表示等待異步代碼執(zhí)行完成帖努。
代碼說明:
Future.delayed(Duration duration, [FutureOr<T> computation()])
這個方法可以便捷地生成一個Future對象,延遲duration
時間執(zhí)行computation()
粪般,并返回一個
T
類型的對象拼余。await
等待Future
返回執(zhí)行完成后結(jié)果,代碼遇到await
會等待 Future 返回結(jié)果亩歹,再執(zhí)行下一句代碼姿搜。
返回值是Future<T>
的方法,如果不加await
捆憎,是立即返回一個Future<T>
對象舅柜;加上await
,則待Future
執(zhí)行完畢后返回T
類型的對象躲惰。async
標(biāo)記一個方法為異步方法致份,使用到 await 關(guān)鍵字的方法必須加 async,否則會提示 "The await expression can only be used in an async function."
捕獲異常
跟同步代碼捕獲異常的方式一樣础拨,使用try-catch氮块,代碼如下:
Future<String> getName() {
return Future.delayed(
Duration(seconds: 2), () => throw Exception('Something wrong'));
}
void printName() async {
try {
var name = await getName();
print(name);
} catch (e) {
print(e.toString());
}
}
void main() {
printName();
print('Something else');
}
輸出結(jié)果: