-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPromise_basics.cpp
More file actions
259 lines (234 loc) · 7.12 KB
/
Promise_basics.cpp
File metadata and controls
259 lines (234 loc) · 7.12 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <cassert>
#include <iostream>
#include <poolqueue/Promise.hpp>
using poolqueue::Promise;
int main() {
// A Promise is default-constructible.
Promise p0;
assert(!p0.settled());
// The key Promise method is then(), which returns a new dependent
// Promise.
Promise p1 = p0.then(
// onFulfil callback
[](const std::string& s) {
std::cout << "p1 fulfilled with " << s << '\n';
return nullptr;
},
// onReject callback
[](const std::exception_ptr& e) {
try {
if (e)
std::rethrow_exception(e);
}
catch (const std::exception& e) {
std::cout << "p1 rejected with " << e.what() << '\n';
}
return nullptr;
});
assert(!p1.settled());
// Settling a Promise invokes the appropriate callback (if present)
// and recursively settles dependent Promises.
p0.settle(std::string("foo"));
assert(p0.settled());
assert(p1.settled());
#if 0
// DON'T DO THIS - A Promise cannot be settled more than once.
p0.settle(std::string("bar"));
// DON'T DO THIS - settle() can never be called on a dependent
// Promise (i.e. one returned by then() or except() methods) even
// if it has not been settled.
p1.settle(std::string("baz"));
// DON'T DO THIS - The onResolve argument type must match the
// upstream type.
p0.then(
// onFulfil callback
[](int i) { // <-- type mismatch
return nullptr;
},
// onReject callback
[](const std::exception_ptr& e) {
return nullptr;
});
// DON'T DO THIS - Promise callbacks must return a value.
p0.then(
[](const std::string& s) {
// missing return value
});
#endif
// then() can be called more than once on a Promise. It is also okay
// to call then() on a settled Promise - in that case the dependent
// Promise settles immediately.
Promise p2 = p0.then(
// onFulfil callback
[](const std::string& s) {
std::cout << "p2 fulfilled with " << s << '\n';
return 0;
},
// onReject callback
[](const std::exception_ptr& e) {
try {
if (e)
std::rethrow_exception(e);
}
catch (const std::exception& e) {
std::cout << "p2 rejected with " << e.what() << '\n';
}
return -1;
});
assert(p0.settled());
assert(p2.settled());
#if 0
// DON'T DO THIS - onFulfil and onReject cannot have different
// return types.
p0.then(
// onFulfil callback
[](const std::string& s) {
return 0;
},
// onReject callback
[](const std::exception_ptr& e) {
return std::string("abc");
});
#endif
// A non-dependent Promise can be created with callbacks.
Promise p3(
// onFulfil callback
[](const std::string& s) {
std::cout << "p3 fulfilled with " << s << '\n';
return 0;
},
// onReject callback
[](const std::exception_ptr& e) {
try {
if (e)
std::rethrow_exception(e);
}
catch (const std::exception& e) {
std::cout << "p3 rejected with " << e.what() << '\n';
}
return -1;
});
assert(!p3.settled());
// Here is how you would settle with an exception.
p3.settle(std::make_exception_ptr(std::runtime_error("bad")));
assert(p3.settled());
// However, the way an exception is typically created is a
// callback throws it.
Promise p4 = p0.then(
// onFulfil callback
[](const std::string& s) {
std::cout << "p4 fulfilled with " << s << '\n';
if ("some error occurs")
throw std::runtime_error("sample error");
return 0;
},
// onReject callback
[](const std::exception_ptr& e) {
// This callback won't get exceptions from the same Promise,
// only from upstream Promises. Only one callback on a
// Promise can execute.
try {
if (e)
std::rethrow_exception(e);
}
catch (const std::exception& e) {
std::cout << "p4 rejected with " << e.what() << '\n';
}
return -1;
});
assert(p4.settled());
// Dependent Promises will get the exception.
Promise p5 = p4.then(
// onFulfil callback
[](int i) {
std::cout << "p5 fulfilled with " << i << '\n';
return 0;
},
// onReject callback
[](const std::exception_ptr& e) {
try {
if (e)
std::rethrow_exception(e);
}
catch (const std::exception& e) {
std::cout << "p5 rejected with " << e.what() << '\n';
}
return -1;
});
assert(p5.settled());
// A Promise can have just one callback. The onReject argument
// to then() can be omitted...
Promise p6 = p4.then(
// onFulfil callback
[](int i) {
// In this case the callback will not be invoked because
// p4's callback threw an exception.
std::cout << "p6 fulfilled with " << i << '\n';
std::unexpected();
return 0;
});
assert(p4.settled());
assert(p6.settled());
// ...and except() works the same as then() but does not take
// an onFulfil argument.
//
// Because p6 was missing the callback that would have been
// invoked, it simply carries forward the result from its
// upstream Promise p4.
Promise p7 = p6.except(
// onReject callback
[](const std::exception_ptr& e) {
try {
if (e)
std::rethrow_exception(e);
}
catch (const std::exception& e) {
std::cout << "p7 rejected with " << e.what() << '\n';
}
return nullptr;
});
assert(p6.settled());
assert(p7.settled());
// Callbacks can always omit their arguments, even if
// the upstream type is non-void.
Promise p8 = p0.then(
// onFulfil callback
[]() {
std::cout << "p8 fulfilled (argument omitted)\n";
return nullptr;
},
// onReject callback
[]() {
std::cout << "p8 rejected (argument omitted)\n";
return nullptr;
});
// Here's what happens if a callback returns a Promise.
Promise p9;
Promise p10 = Promise().settle().then(
[=]() {
return p9;
});
assert(!p9.settled());
assert(!p10.settled());
#if 0
// DON'T DO THIS - A returned Promise does not propagate like other
// types. A callback should never take a Promise argument.
p10.then([](const Promise& value) {
return nullptr;
});
#endif
// Dependent Promises will be settled with the result
// of the returned Promise, whenever the returned
// Promise settles. That could be either immediately
// (i.e. if the returned Promise is already settled)
// or sometime in the future.
p9.settle(std::string("bar"));
assert(p9.settled());
assert(p10.settled());
Promise p11 = p10.then(
[](const std::string& s) {
std::cout << "p11 fulfilled with " << s << '\n';
return nullptr;
});
return 0;
}