Synchronously wait for a promise to complete (ONLY safe in top-level contexts)
- Template Parameters
-
| T | The type of value the promise resolves to (automatically deduced) |
- Parameters
-
- 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");
const Response& resp =
result.value();
FL_WARN(
"Success: " << resp.text());
} else {
}
PromiseResult< T > await_top_level(Promise< T > p) FL_NO_EXCEPT
Synchronously wait for a promise to complete (ONLY safe in top-level contexts)
expected< T, E > result
Alias for expected (Rust-style naming)
Definition at line 193 of file executor.h.
193 {
194
197 }
198
199
202 return PromiseResult<T>(p.
value());
203 } else {
204 return PromiseResult<T>(p.
error());
205 }
206 }
207
208
210 if (await_depth > 10) {
212 }
213
214 ++await_depth;
215
216
217 int pump_count = 0;
218 const int max_pump_iterations = 10000;
219
220 while (!p.
is_completed() && pump_count < max_pump_iterations) {
221
223
224
226 break;
227 }
228
229
231
232 ++pump_count;
233 }
234
235 --await_depth;
236
237
238 if (pump_count >= max_pump_iterations) {
240 }
241
242
245 } else {
247 }
248}
const Error & error() const FL_NO_EXCEPT
Get the error (only valid if is_rejected() returns true)
bool valid() const FL_NO_EXCEPT
Check if Promise is valid.
void update() FL_NO_EXCEPT
Update Promise state in main loop - should be called periodically This processes pending callbacks wh...
const T & value() const FL_NO_EXCEPT
Get the result value (only valid if is_resolved() returns true)
bool is_completed() const FL_NO_EXCEPT
Check if Promise is completed (resolved or rejected)
bool is_resolved() const FL_NO_EXCEPT
Check if Promise is resolved (completed successfully)
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().