FastLED 3.9.15
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(X)
Definition log.h:276
PromiseResult< T > await_top_level(Promise< T > p)
Synchronously wait for a promise to complete (ONLY safe in top-level contexts)
Definition executor.h:186
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31

Definition at line 186 of file executor.h.

186 {
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}
bool is_completed() const FL_NOEXCEPT
Check if Promise is completed (resolved or rejected)
Definition promise.h:140
const T & value() const FL_NOEXCEPT
Get the result value (only valid if is_resolved() returns true)
Definition promise.h:158
const Error & error() const FL_NOEXCEPT
Get the error (only valid if is_rejected() returns true)
Definition promise.h:167
bool valid() const FL_NOEXCEPT
Check if Promise is valid.
Definition promise.h:108
void update() FL_NOEXCEPT
Update Promise state in main loop - should be called periodically This processes pending callbacks wh...
Definition promise.h:134
bool is_resolved() const FL_NOEXCEPT
Check if Promise is resolved (completed successfully)
Definition promise.h:146
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().

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