Created
July 12, 2015 14:29
-
-
Save JesterXL/8b024166230dc30a5b2a to your computer and use it in GitHub Desktop.
Examples of await/async using Dart to illustrate strange function order.
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
void testBasicPromises() | |
{ | |
// 1, 3, 2 | |
void basic() | |
{ | |
print("1"); | |
new Future.delayed(new Duration(seconds: 3), () | |
{ | |
print("2"); | |
}); | |
print("3"); | |
} | |
void settingData() | |
{ | |
var data = null; | |
new Future.delayed(new Duration(seconds: 3), () | |
{ | |
data = ["uno", "dos", "tres"]; | |
}); | |
print("data: $data"); | |
} | |
// 1, 3, 2, 4 | |
void advancedSettingData() async | |
{ | |
var data = null; | |
print("1"); | |
void getData() async | |
{ | |
return new Future.delayed(new Duration(seconds: 3), () | |
{ | |
print("2"); | |
return ["uno", "dos", "tres"]; | |
}); | |
} | |
print("3"); | |
data = await getData(); | |
print("4"); | |
print("data: $data"); | |
} | |
// basic(); | |
// settingData(); | |
advancedSettingData(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment