FastLED 3.9.15
Loading...
Searching...
No Matches

◆ await_top_level()

template<typename T>
fl::result< T > fl::await_top_level ( fl::promise< T > promise)

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
promiseThe promise to wait for
Returns
A PromiseResult containing either the resolved value T or an Error

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

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

  • Promise callbacks (.then, .catch_)
  • Nested async operations
  • Interrupt handlers
  • Library initialization code

The "_top_level" suffix emphasizes this safety requirement.

Type Deduction: The template parameter T is automatically deduced from the promise parameter, so you don't need to specify it explicitly.

Usage

auto promise = fl::fetch_get("http://example.com");
auto result = fl::await_top_level(promise); // Type automatically deduced!
if (result.ok()) {
const Response& resp = result.value();
FL_WARN("Success: " << resp.text());
} else {
FL_WARN("Error: " << result.error().message);
}
// Or use operator bool
if (result) {
FL_WARN("Got response: " << result.value().status());
}
// You can still specify the type explicitly if needed:
auto explicit_result = fl::await_top_level<response>(promise);
Promise class that provides fluent .then() and .catch_() semantics This is a lightweight wrapper arou...
Definition promise.h:72
const T & value() const
Get the success value (const)
bool ok() const
Check if the result is successful.
Result type for promise operations.
fl::result< T > await_top_level(fl::promise< T > promise)
Synchronously wait for a promise to complete (ONLY safe in top-level contexts)
Definition async.h:175
fl::promise< response > fetch_get(const fl::string &url, const fetch_options &request)
HTTP GET request.
Definition fetch.cpp:180
#define FL_WARN
Definition warn.h:12

Definition at line 175 of file async.h.

175 {
176 // Handle invalid promises
177 if (!promise.valid()) {
178 return fl::result<T>(Error("Invalid promise"));
179 }
180
181 // If already completed, return immediately
182 if (promise.is_completed()) {
183 if (promise.is_resolved()) {
184 return fl::result<T>(promise.value());
185 } else {
186 return fl::result<T>(promise.error());
187 }
188 }
189
190 // Track recursion depth to prevent infinite loops
191 static fl::ThreadLocal<int> await_depth(0);
192 if (await_depth.access() > 10) {
193 return fl::result<T>(Error("await_top_level recursion limit exceeded - possible infinite loop"));
194 }
195
196 ++await_depth.access();
197
198 // Wait for promise to complete while pumping async tasks
199 int pump_count = 0;
200 const int max_pump_iterations = 10000; // Safety limit
201
202 while (!promise.is_completed() && pump_count < max_pump_iterations) {
203 // Update the promise first (in case it's not managed by async system)
204 promise.update();
205
206 // Check if completed after update
207 if (promise.is_completed()) {
208 break;
209 }
210
211 // Platform-agnostic async pump and yield
212 async_yield();
213
214 ++pump_count;
215 }
216
217 --await_depth.access();
218
219 // Check for timeout
220 if (pump_count >= max_pump_iterations) {
221 return fl::result<T>(Error("await_top_level timeout - promise did not complete"));
222 }
223
224 // Return the result
225 if (promise.is_resolved()) {
226 return fl::result<T>(promise.value());
227 } else {
228 return fl::result<T>(promise.error());
229 }
230}
const Error & error() const
Get the error (only valid if is_rejected() returns true)
Definition promise.h:181
void update()
Update promise state in main loop - should be called periodically This processes pending callbacks wh...
Definition promise.h:148
bool is_resolved() const
Check if promise is resolved (completed successfully)
Definition promise.h:160
bool valid() const
Check if promise is valid.
Definition promise.h:122
const T & value() const
Get the result value (only valid if is_resolved() returns true)
Definition promise.h:172
bool is_completed() const
Check if promise is completed (resolved or rejected)
Definition promise.h:154
void async_yield()
Platform-specific async yield function.
Definition async.cpp:68
ThreadLocalFake< T > ThreadLocal
Error type for promises.
Definition promise.h:53

References fl::ThreadLocalFake< T >::access(), async_yield(), fl::promise< T >::error(), fl::promise< T >::is_completed(), fl::promise< T >::is_resolved(), fl::promise< T >::update(), fl::promise< T >::valid(), and fl::promise< T >::value().

Referenced by test_await_approach(), and test_json_await().

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