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
-
promise | The 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
FL_WARN(
"Success: " << resp.text());
} else {
FL_WARN(
"Error: " << result.error().message);
}
}
Promise class that provides fluent .then() and .catch_() semantics This is a lightweight wrapper arou...
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)
fl::promise< response > fetch_get(const fl::string &url, const fetch_options &request)
HTTP GET request.
Definition at line 175 of file async.h.
175 {
176
179 }
180
181
185 } else {
187 }
188 }
189
190
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
199 int pump_count = 0;
200 const int max_pump_iterations = 10000;
201
203
205
206
208 break;
209 }
210
211
213
214 ++pump_count;
215 }
216
217 --await_depth.access();
218
219
220 if (pump_count >= max_pump_iterations) {
221 return fl::result<T>(
Error(
"await_top_level timeout - promise did not complete"));
222 }
223
224
227 } else {
229 }
230}
const Error & error() const
Get the error (only valid if is_rejected() returns true)
void update()
Update promise state in main loop - should be called periodically This processes pending callbacks wh...
bool is_resolved() const
Check if promise is resolved (completed successfully)
bool valid() const
Check if promise is valid.
const T & value() const
Get the result value (only valid if is_resolved() returns true)
bool is_completed() const
Check if promise is completed (resolved or rejected)
void async_yield()
Platform-specific async yield function.
ThreadLocalFake< T > ThreadLocal
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().