Created
May 22, 2020 12:22
-
-
Save 410063005/a4d7e2adcfa0006f5bb5504ef545ecf3 to your computer and use it in GitHub Desktop.
一个用于展示 Flutter Future.wait() 用法的代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main(List<String> args) async { | |
print('hello2'); | |
var t1 = DateTime.now().millisecondsSinceEpoch; | |
// replace callFuns() with callFuns2() | |
await callFuns(); | |
var t2 = DateTime.now().millisecondsSinceEpoch; | |
print('cost ${t2 - t1}'); | |
} | |
// cost 5015 | |
Future callFuns() async { | |
var r = await fun1(); | |
var r2 = await fun2(); | |
print('r = $r'); | |
print('r2 = $r2'); | |
} | |
// cost 3008 | |
Future callFuns2() async { | |
//var r = await fun1(); | |
//var r2 = await fun2(); | |
var rArr = await Future.wait([fun1(), fun2()]); | |
var r = rArr[0]; | |
var r2 = rArr[1]; | |
print('r = $r'); | |
print('r2 = $r2'); | |
} | |
Future<String> fun1() { | |
return Future.delayed(Duration(seconds: 2), () { | |
return "future 1"; | |
}); | |
} | |
Future<String> fun2() { | |
return Future.delayed(Duration(seconds: 3), () { | |
return "future 2"; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment