Last active
June 14, 2017 19:15
Asynchronous with await and Future API
This file contains hidden or 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
import "dart:async"; | |
Future<DateTime> currentTime(int forExp) async { | |
DateTime now; | |
for (int i = 0; i < 3; i++) { | |
now = new DateTime.now(); | |
print("- exp${forExp}:current-time[${i}] = ${now}"); | |
} | |
return now; | |
} | |
Future<Object> exp1() async { | |
print("* exp1:start"); | |
DateTime now = await currentTime(1); | |
print("* exp1:result=${now}"); | |
return null; | |
} | |
Future<Object> exp2() async { | |
print("* exp2:start"); | |
Future<Object> result = currentTime(2); | |
result | |
..then((DateTime now) { | |
print("* exp2:result=${now}"); | |
}) | |
..whenComplete(() { | |
print("* exp2:complete"); | |
}); | |
print("* exp2:reaching-flow-end"); | |
return result; | |
} | |
void main() { | |
Future.wait([exp1(), exp2()]).whenComplete(() { | |
print("all future complete"); | |
}); | |
print("reach end of main function"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment