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

◆ test_promise_approach()

void test_promise_approach ( )

Definition at line 109 of file NetTestReal.h.

109 {
110 FL_WARN("APPROACH 1: Promise-based pattern with explicit types");
111
112 // TUTORIAL: fetch_get() returns fl::promise<fl::response> (not auto!)
113 // The promise represents a future HTTP response that may succeed or fail
114 // Chain .then() for success handling and the lambda receives a
115 // const fl::response& when the fetch succeeds. error_ will handle network device
116 // failures (no connection, DNS failure, etc, but not HTTP errors like 404, 500, etc.)
117 fl::fetch_get("http://fastled.io").then([](const fl::response& response) {
118 // TUTORIAL: Check if HTTP request was successful
119 if (response.ok()) {
120 FL_WARN("SUCCESS [Promise] HTTP fetch successful! Status: "
121 << response.status() << " " << response.status_text());
122
123 // TUTORIAL: get_content_type() returns fl::optional<fl::string>
124 // Optional types may or may not contain a value - always check!
126 if (content_type.has_value()) {
127 FL_WARN("CONTENT [Promise] Content-Type: " << *content_type);
128 }
129
130 // TUTORIAL: response.text() returns fl::string with response body
131 const fl::string& response_body = response.text();
132 if (response_body.length() >= 100) {
133 FL_WARN("RESPONSE [Promise] First 100 characters: " << response_body.substr(0, 100));
134 } else {
135 FL_WARN("RESPONSE [Promise] Full response (" << response_body.length()
136 << " chars): " << response_body);
137 }
138
139 // Visual feedback: Green LEDs indicate promise-based success
140 fill_solid(leds, NUM_LEDS, CRGB(0, 64, 0)); // Green for promise success
141 } else {
142 // HTTP error (like 404, 500, etc.) - still a valid response, just an error status
143 FL_WARN("ERROR [Promise] HTTP Error! Status: "
144 << response.status() << " " << response.status_text());
145 FL_WARN("CONTENT [Promise] Error content: " << response.text());
146
147 // Visual feedback: Orange LEDs indicate HTTP error
148 fill_solid(leds, NUM_LEDS, CRGB(64, 32, 0)); // Orange for HTTP error
149 }
150 })
151 // TUTORIAL: Chain .catch_() for network/connection error handling
152 // The lambda receives a const fl::Error& when the fetch fails completely
153 .catch_([](const fl::Error& network_error) {
154 // Network error (no connection, DNS failure, etc.)
155 FL_WARN("ERROR [Promise] Network Error: " << network_error.message);
156
157 // Visual feedback: Red LEDs indicate network failure
158 fill_solid(leds, NUM_LEDS, CRGB(64, 0, 0)); // Red for network error
159 });
160}
CRGB leds[NUM_LEDS]
#define NUM_LEDS
bool has_value() const
Definition optional.h:30
StrN substr(fl::size start, fl::size length) const
Definition str.h:543
fl::size length() const
Definition str.h:325
fl::optional< fl::string > get_content_type() const
Get content type convenience method.
Definition fetch.h:112
const fl::string & status_text() const
HTTP status text (like JavaScript response.statusText)
Definition fetch.h:94
int status() const
HTTP status code (like JavaScript response.status)
Definition fetch.h:91
bool ok() const
Check if response is successful (like JavaScript response.ok)
Definition fetch.h:97
const fl::string & text() const
Response body as text (like JavaScript response.text())
Definition fetch.h:100
HTTP response class (unified interface)
Definition fetch.h:83
void fill_solid(struct CRGB *targetArray, int numToFill, const struct CRGB &color)
Fill a range of LEDs with a solid color.
Definition fill.cpp:9
Optional< T > optional
Definition optional.h:14
fl::promise< response > fetch_get(const fl::string &url, const fetch_options &request)
HTTP GET request.
Definition fetch.cpp:180
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86
fl::string message
Definition promise.h:54
Error type for promises.
Definition promise.h:53
#define FL_WARN
Definition warn.h:12

References fl::fetch_get(), fl::fill_solid(), FL_WARN, fl::response::get_content_type(), fl::Optional< T >::has_value(), leds, fl::StrN< SIZE >::length(), fl::Error::message, NUM_LEDS, fl::response::ok(), fl::response::status(), fl::response::status_text(), fl::StrN< SIZE >::substr(), and fl::response::text().

Referenced by loop().

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