Dart syntax- Future,asyc,await

import 'dart:io';

void main() {
performTasks();
}

void performTasks() async {//i have someone need a wait
team1();
String task2result = await team2(); //my team there is someone need to wait.
team3(task2result);
}

void team1() {
print('task 1 complete');
}

Future team2() async {//in the future team2, there is someone who is call "await" need to wait.
String result;
print('there is team2. Someone who is call "await" need a wait.');

await Future.delayed( //pls wait for me.
Duration(seconds: 3,), () {
result = 'task 2 await\'s Data.';
print('task 2 complete. \'await\' is giving his data to team3.');
},
);
return result;
}

void team3(String task2Data) {
print('task 3 complete, i got $task2Data');
}

asychrony 

等可能很耗時的程式設置好後再返回future/steam物件。從出發到結果中間無await標示的可以先執行。

☝Future async + await 2.使用Futurn type 必須先聲明async

☝Steam 有兩個方法 async + await for迴圈。 2. steam API。

not use await for for UI event listeners .因為它會無止盡地傳送steams.

☝Generator 產生一系列的值。
1.同步產生使用 Iterable + sync*, 由yield傳送值。
2.非同步產生使用steam + async*,由yield傳送值。
若命名有重覆可使用 yield*

Comments

Popular posts from this blog

Go-VSCode -Autocomplete and Auto-import

Go - mail