MCPcopy Create free account
hub / github.com/FastLED/FastLED / await_top_level

Function await_top_level

src/fl/task/executor.h:186–241  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

184/// @endcode
185template<typename T>
186PromiseResult<T> await_top_level(Promise<T> p) {
187 // Handle invalid promises
188 if (!p.valid()) {
189 return PromiseResult<T>(Error("Invalid promise"));
190 }
191
192 // If already completed, return immediately
193 if (p.is_completed()) {
194 if (p.is_resolved()) {
195 return PromiseResult<T>(p.value());
196 } else {
197 return PromiseResult<T>(p.error());
198 }
199 }
200
201 // Track recursion depth to prevent infinite loops
202 int& await_depth = detail::await_depth_tls();
203 if (await_depth > 10) {
204 return PromiseResult<T>(Error("await_top_level recursion limit exceeded - possible infinite loop"));
205 }
206
207 ++await_depth;
208
209 // Wait for promise to complete while pumping async tasks
210 int pump_count = 0;
211 const int max_pump_iterations = 10000; // Safety limit
212
213 while (!p.is_completed() && pump_count < max_pump_iterations) {
214 // Update the promise first (in case it's not managed by async system)
215 p.update();
216
217 // Check if completed after update
218 if (p.is_completed()) {
219 break;
220 }
221
222 // Platform-agnostic async pump and yield
223 run(1000);
224
225 ++pump_count;
226 }
227
228 --await_depth;
229
230 // Check for timeout
231 if (pump_count >= max_pump_iterations) {
232 return PromiseResult<T>(Error("await_top_level timeout - promise did not complete"));
233 }
234
235 // Return the result
236 if (p.is_resolved()) {
237 return PromiseResult<T>(p.value());
238 } else {
239 return PromiseResult<T>(p.error());
240 }
241}
242
243} // namespace task

Callers 7

FL_TEST_FILEFunction · 0.85
mainFunction · 0.85
test_json_endpointFunction · 0.85
test_get_endpointFunction · 0.85
test_ping_endpointFunction · 0.85
test_await_approachFunction · 0.85
test_json_awaitFunction · 0.85

Calls 8

ErrorClass · 0.85
runFunction · 0.70
validMethod · 0.45
is_completedMethod · 0.45
is_resolvedMethod · 0.45
valueMethod · 0.45
errorMethod · 0.45
updateMethod · 0.45

Tested by 5

test_json_endpointFunction · 0.68
test_get_endpointFunction · 0.68
test_ping_endpointFunction · 0.68
test_await_approachFunction · 0.68
test_json_awaitFunction · 0.68