FastLED 3.10.8
Loading...
Searching...
No Matches

◆ await_top_level()

template<typename T>
PromiseResult< T > fl::task::await_top_level ( Promise< T > p)

Synchronously wait for a promise to complete (ONLY safe in top-level contexts)

Template Parameters
TThe type of value the promise resolves to (automatically deduced)
Parameters
pThe promise to wait for
Returns
A promise_result containing either the resolved value T or an Error

This function blocks until the promise is either resolved or rejected, then returns a promise_result that can be checked with ok() for success/failure. While waiting, it continuously calls run(1000) to pump async tasks and yield appropriately.

PERFORMANCE NOTE: This function busy-waits (high CPU usage) while the promise is pending. For coroutines on ESP32 or Host/Stub platforms, prefer fl::task::await() which truly blocks the coroutine with zero CPU overhead.

SAFETY WARNING: This function should ONLY be called from top-level contexts like Arduino loop() function. Never call this from:

The "_top_level" suffix emphasizes this safety requirement.

Examples

auto p = fl::fetch_get("http://example.com");
auto result = fl::task::await_top_level(p); // Type automatically deduced!
if (result.ok()) {
const Response& resp = result.value();
FL_WARN("Success: " << resp.text());
} else {
FL_WARN("Error: " << result.error().message);
}
#define FL_WARN(...)
Definition log.h:437
PromiseResult< T > await_top_level(Promise< T > p) FL_NO_EXCEPT
Synchronously wait for a promise to complete (ONLY safe in top-level contexts)
Definition executor.h:193
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31

Definition at line 193 of file executor.h.

193 {
194 // Handle invalid promises
195 if (!p.valid()) {
196 return PromiseResult<T>(Error("Invalid promise"));
197 }
198
199 // If already completed, return immediately
200 if (p.is_completed()) {
201 if (p.is_resolved()) {
202 return PromiseResult<T>(p.value());
203 } else {
204 return PromiseResult<T>(p.error());
205 }
206 }
207
208 // Track recursion depth to prevent infinite loops
209 int& await_depth = detail::await_depth_tls();
210 if (await_depth > 10) {
211 return PromiseResult<T>(Error("await_top_level recursion limit exceeded - possible infinite loop"));
212 }
213
214 ++await_depth;
215
216 // Wait for promise to complete while pumping async tasks
217 int pump_count = 0;
218 const int max_pump_iterations = 10000; // Safety limit
219
220 while (!p.is_completed() && pump_count < max_pump_iterations) {
221 // Update the promise first (in case it's not managed by async system)
222 p.update();
223
224 // Check if completed after update
225 if (p.is_completed()) {
226 break;
227 }
228
229 // Platform-agnostic async pump and yield
230 run(1000);
231
232 ++pump_count;
233 }
234
235 --await_depth;
236
237 // Check for timeout
238 if (pump_count >= max_pump_iterations) {
239 return PromiseResult<T>(Error("await_top_level timeout - promise did not complete"));
240 }
241
242 // Return the result
243 if (p.is_resolved()) {
244 return PromiseResult<T>(p.value());
245 } else {
246 return PromiseResult<T>(p.error());
247 }
248}
const Error & error() const FL_NO_EXCEPT
Get the error (only valid if is_rejected() returns true)
Definition promise.h:167
bool valid() const FL_NO_EXCEPT
Check if Promise is valid.
Definition promise.h:108
void update() FL_NO_EXCEPT
Update Promise state in main loop - should be called periodically This processes pending callbacks wh...
Definition promise.h:134
const T & value() const FL_NO_EXCEPT
Get the result value (only valid if is_resolved() returns true)
Definition promise.h:158
bool is_completed() const FL_NO_EXCEPT
Check if Promise is completed (resolved or rejected)
Definition promise.h:140
bool is_resolved() const FL_NO_EXCEPT
Check if Promise is resolved (completed successfully)
Definition promise.h:146
Result type for promise operations.
int & await_depth_tls() FL_NO_EXCEPT
Get reference to thread-local await recursion depth.
void run(fl::u32 microseconds, ExecFlags flags) FL_NO_EXCEPT
Run selected task subsystems.

References await_top_level(), and fl::FL_NO_EXCEPT.

Referenced by await_top_level(), test_await_approach(), test_get_endpoint(), test_json_await(), test_json_endpoint(), and test_ping_endpoint().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: