Dart基礎(chǔ)--異步的實(shí)現(xiàn)

Dart是單線程語(yǔ)言,但在某些特定場(chǎng)景我們需要它是異步的,所以產(chǎn)生了兩個(gè)概念
1.Future:(類似ES6的promise)
2.async:用來(lái)修飾需要異步執(zhí)行的函數(shù),常配合await使用
下面可以看看線程在遇到這些關(guān)鍵字時(shí)候的執(zhí)行順序或任務(wù)隊(duì)列

一.Future的使用

//引入async庫(kù)
import 'dart:async';
//通過(guò)以下那些Future構(gòu)造函數(shù)生成的Future對(duì)象其實(shí)控制權(quán)不在你這里。
//它什么時(shí)候執(zhí)行完畢只能等系統(tǒng)調(diào)度了。你只能被動(dòng)的等待Future執(zhí)行完畢然后調(diào)用你設(shè)置的回調(diào)

now(){
  print('111');
  //通過(guò)then串起來(lái)的那些回調(diào)函數(shù)在Future完成的時(shí)候會(huì)被立即執(zhí)行,也就是說(shuō)它們是同步執(zhí)行,而不是被調(diào)度異步執(zhí)行
  Future(()=>{print("222")})
    .then((res)=>{print("callback01")})
    .then((res)=>{print("callback02")})
    .catchError((error)=>print('錯(cuò)誤捕獲'))
    .whenComplete(()=> print('無(wú)論報(bào)錯(cuò)否一定執(zhí)行'));
  print('333');
  //通過(guò)Future.value()實(shí)例化的Future會(huì)被調(diào)度到微任務(wù)隊(duì)列異步完成
  Future.microtask(() => print('微任務(wù)隊(duì)列里運(yùn)行的Future'));
  //Future.delayed實(shí)例化的Future不會(huì)同步執(zhí)行,會(huì)被調(diào)度到事件隊(duì)列異步執(zhí)行
  Future.delayed(const Duration(seconds:6), () => print('6秒后運(yùn)行的Future'));
  Future.delayed(const Duration(seconds:0), () => print('0秒后運(yùn)行的Future'));
  //通過(guò)Future.sync()實(shí)例化的Future會(huì)同步執(zhí)行其入?yún)⒑瘮?shù),然后(除非這個(gè)入?yún)⒑瘮?shù)返回一個(gè)Future)調(diào)度到微任務(wù)隊(duì)列來(lái)完成自己
  Future.sync(() => print('同步運(yùn)行的Future'));
}

main(){
  now();
}
//此處代碼執(zhí)行順序:
111
333
同步運(yùn)行的Future
微任務(wù)隊(duì)列里運(yùn)行的Future
222
callback01
callback02
無(wú)論報(bào)錯(cuò)否一定執(zhí)行
0秒后運(yùn)行的Future
6秒后運(yùn)行的Future

二.async函數(shù)的使用

優(yōu)點(diǎn):
Future相對(duì)于調(diào)度回調(diào)函數(shù)來(lái)說(shuō),緩減了回調(diào)地獄的問(wèn)題。
但是如果Future要串起來(lái)的的東西比較多的話,代碼還是會(huì)可讀性比較差。特別是各種Future嵌套起來(lái),是比較燒腦的。

特點(diǎn):
1.使用async修飾的函數(shù)視為內(nèi)有異步操作
2.遇到await程序會(huì)認(rèn)為后面有延時(shí)操作或等待操作
3.代碼執(zhí)行到async函數(shù)的第一個(gè)await關(guān)鍵字語(yǔ)句會(huì)暫停函數(shù)內(nèi)以下代碼,拋出一個(gè)Future,釋放部分空間到函數(shù)調(diào)用的地方向下繼續(xù)執(zhí)行,執(zhí)行完畢在回來(lái)執(zhí)行await下面的語(yǔ)句--可見第五個(gè)demo(不太好闡述,所以配合代碼體現(xiàn))

1.第一個(gè)demo

now() async{
  print('111');
  String value=await outValue();
  print('now--$value');
}

outValue(){
  print("print outValue");
  return "hello outValue";
}

main(){
  print('main E');
  now();
  print('main X');
}
//執(zhí)行順序
main E
111
print outValue
main X
now--hello outValue

2.第二個(gè)demo

getData () async{
  String str=await "123";
  print('getData');
}

create(){
  getData();
  print('create');
}

main(){
  create();
}
//執(zhí)行順序
create
getData

3.第三個(gè)demo

main() {
  _startMethod();
  _method_C();
}

_startMethod() async{
  _method_A();
  await _method_B();
  print("start結(jié)束");
}
_method_A(){
  print("A開始執(zhí)行這個(gè)方法~");

}

_method_B() async {
  print("B開始執(zhí)行這個(gè)方法~");
  await print("后面執(zhí)行這句話~");
  await print("繼續(xù)執(zhí)行這句哈11111~");
}

_method_C(){
  print("C開始");
}
//執(zhí)行順序
A開始執(zhí)行這個(gè)方法~
B開始執(zhí)行這個(gè)方法~
后面執(zhí)行這句話~
C開始
繼續(xù)執(zhí)行這句哈11111~
start結(jié)束

4.第四個(gè)demo

create(){
  print("start");
  a();
  b();
  c();
  print("end");
}

a() {
  print('a1');
  String a= "a2";
  print("$a");
}

b() {
  print('b1');
  String b= "b2";
  print("$b");
}

c() {
  print('c1');
  String c= "c2";
  print("$c");
}

main(){
  create();
}
//執(zhí)行順序
start
a1
a2
b1
b2
c1
c2
end

5.第五個(gè)demo

create(){
  print("start");
  a();
  b();
  c();
  print("end");
}

a() async{
  print('a1');
  String a=await "a2";
  print("$a");
}

b() async{
  print('b1');
  String b=await "b2";
  print("$b");
}

c() async{
  print('c1');
  String c=await "c2";
  print("$c");
}

main(){
  create();
}
//執(zhí)行順序
start
a1
b1
c1
end
a2
b2
c2

6.第六個(gè)demo

import 'dart:convert' as convert;
import 'package:http/http.dart' as http;

create() async{
  print("start");
  a();
  print("end");
}

a() async{
  print('a1');
  String url="http://localhost:3000/myServer1";
  var res=await http.get(url);
  print('請(qǐng)求結(jié)果${res.body}');
  await b();
  print("a2");
}

b() async{
  print('b1');
  await c();
  print("b2");
}

c() async{
  print('c1');
  await print("c2");
  print('a3');
}

main(){
  create();
}
//執(zhí)行順序
start
a1
end
請(qǐng)求結(jié)果{"code":0,"data":"myServer1","message":"獲取數(shù)據(jù)"}
b1
c1
c2
a3
b2
a2
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容