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)
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 186 of file executor.h.
186 {
187
190 }
191
192
195 return PromiseResult<T>(p.
value());
196 } else {
197 return PromiseResult<T>(p.
error());
198 }
199 }
200
201
203 if (await_depth > 10) {
205 }
206
207 ++await_depth;
208
209
210 int pump_count = 0;
211 const int max_pump_iterations = 10000;
212
213 while (!p.
is_completed() && pump_count < max_pump_iterations) {
214
216
217
219 break;
220 }
221
222
224
225 ++pump_count;
226 }
227
228 --await_depth;
229
230
231 if (pump_count >= max_pump_iterations) {
233 }
234
235
238 } else {
240 }
241}
bool is_completed() const FL_NOEXCEPT
Check if Promise is completed (resolved or rejected)
const T & value() const FL_NOEXCEPT
Get the result value (only valid if is_resolved() returns true)
const Error & error() const FL_NOEXCEPT
Get the error (only valid if is_rejected() returns true)
bool valid() const FL_NOEXCEPT
Check if Promise is valid.
void update() FL_NOEXCEPT
Update Promise state in main loop - should be called periodically This processes pending callbacks wh...
bool is_resolved() const FL_NOEXCEPT
Check if Promise is resolved (completed successfully)
Result type for promise operations.
int & await_depth_tls()
Get reference to thread-local await recursion depth.
void run(fl::u32 microseconds, ExecFlags flags)
Run selected task subsystems.
References fl::task::detail::await_depth_tls(), fl::Error, fl::task::Promise< T >::error(), fl::task::Promise< T >::is_completed(), fl::task::Promise< T >::is_resolved(), run(), fl::task::Promise< T >::update(), fl::task::Promise< T >::valid(), and fl::task::Promise< T >::value().
Referenced by test_await_approach(), test_get_endpoint(), test_json_await(), test_json_endpoint(), and test_ping_endpoint().