/
futures.dart
175 lines (139 loc) · 3.75 KB
/
futures.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//Entry point function
void main() {
//Run Example#1.
makeDataCall();
//Run Example#2.
//makeDataCallWithException();
//Run Example#3.
//sequencingOperations();
//Run Example#4.
//mainWithFutureAPI();
//Run Example#5.
//mainWithFutureAPIVoid();
//Run Example#6.
//mainWithFutureAPIError();
//Run Example#7.
//mainWithFutureAPIWait();
}
//Example#1. Fetching data asynchronously
// Expensive function could be a function that takes
// long time to process data and return results.
// Assume this function takes long time to return in real-world.
String getExpansiveData() {
return "I'm expansive data";
}
// This is the asynchronous function that makes the expensive
// data call and prints the results.
Future<void> makeDataCall() async {
var data = await getExpansiveData();
print(data);
}
//----END----//
//Example#2. Demonstrating handling exception thrown in asynchronous operations
//Expansive operations ended up throwing exception
String getExpansiveDataWithException() {
throw Exception("Error occurred in fetching data");
}
//Asynchronous function that makes the expensive data call
Future<void> makeDataCallWithException() async {
try {
await getExpansiveDataWithException();
} catch (e) {
print(e.toString());
}
}
//----END----//
//Example#3. Sequencing order of asynchronous calls
void getDataA() {
print("dataA");
}
void getDataB() {
print("dataB");
}
String getDataC() {
return "dataC";
}
//Entry point function
Future<void> sequencingOperations() async {
//Order matters.
//Functions will execute in the order they were called.
await getDataA();
await getDataB();
//getDataC() will execute first and will
//pass its data into print() function
print(await getDataC());
}
//----END----//
//Example #4. Using Future API (Future is returning String type)
//Future with String data is being returned.
//This function returns the instance of
//Future and not the actual data.
Future<String> makeDataCallFutureString() async {
return await getExpansiveData();
}
void mainWithFutureAPI() {
var theFuture = makeDataCallFutureString();
//then() is called at the instance of Future
theFuture.then((value) {
print(value);
});
}
//----END----//
//Example#5. Using Future API (Future is returning void type)
//Future doesn't return anything
Future<void> makeDataCallFutureVoid() async {
await getExpansiveData();
}
void mainWithFutureAPIVoid() {
var theFuture = makeDataCallFutureVoid();
//then() uses underscore as unused argument.
theFuture.then((_) {
//_ is not used
print("There's nothing to be printed here. Work is already done.");
});
}
//----END----//
//Example#6. Future API - Error Handling
//Future with String data is being returned.
//This function returns the instance of
//Future and not the actual data.
Future<String> makeDataCallFutureAPIError() async {
var data = await getExpansiveData();
throw Exception("Error occurred in making data call: $data");
}
void mainWithFutureAPIError() {
var theFuture = makeDataCallFutureAPIError();
//Error is caught and handled in catchError block
theFuture.then((value) {
print(value);
}).catchError((error) {
print(error);
});
}
//----END----//
//Example#7. Using `Future.wait()`
String getData(data) {
return data;
}
Future<String> getDataAFuture() async {
return await getData("dataA");
}
Future<String> getDataBFuture() async {
return await getData("dataB");
}
Future<String> getDataCFuture() async {
return await getData("dataC");
}
mainWithFutureAPIWait() async {
//Chaining Futures in order
await Future.wait([
getDataAFuture(),
getDataBFuture(),
getDataCFuture(),
])
.then(
(List responses) => print(responses),
)
.catchError((error) => print(error));
}
//----END----//