Skip to content

Instantly share code, notes, and snippets.

@JesterXL
Created July 12, 2015 14:29
Show Gist options
  • Save JesterXL/8b024166230dc30a5b2a to your computer and use it in GitHub Desktop.
Save JesterXL/8b024166230dc30a5b2a to your computer and use it in GitHub Desktop.
Examples of await/async using Dart to illustrate strange function order.
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