FastLED 3.9.15
Loading...
Searching...
No Matches
Client.ino File Reference

Detailed Description

Educational tutorial for FastLED HTTP fetch API with explicit types.

Author
FastLED Community

This tutorial demonstrates network functionality in FastLED WASM builds, specifically the fetch API for making HTTP requests. It shows two different approaches for handling asynchronous operations with EXPLICIT TYPES for educational clarity.

EDUCATIONAL FOCUS: All types are explicitly declared instead of using 'auto' to help you understand the FastLED type system and async patterns.

TWO ASYNC APPROACHES DEMONSTRATED:

APPROACH 1: Promise-based with .then() and .catch_() callbacks (JavaScript-like)

  • Uses method chaining for async operations
  • Callbacks handle success and error cases
  • Non-blocking, event-driven pattern

APPROACH 2: fl::task::await_top_level() pattern for synchronous-style async code

The example toggles between these approaches every 10 seconds to demonstrate both patterns working with the same underlying fetch API.

FASTLED ASYNC TYPE SYSTEM TUTORIAL:

Key Types You'll Learn:

NEW FETCH API STRUCTURE:

  • FetchOptions is a pure data configuration object
  • fetch_get() returns fl::task::Promise<fl::net::http::Response> (not auto!)
  • Promises can be handled with .then()/.catch_() OR await_top_level()
  • All async operations integrate with FastLED's engine automatically

EXPLICIT TYPE EXAMPLES:

Promise-based approach:

promise.then([](const fl::net::http::Response& response) { /* handle success *&zwj;/ })
.catch_([](const fl::task::Error& error) { /* handle error *&zwj;/ });
HTTP response class (unified interface)
Definition fetch.h:78
Promise & then(fl::function< void(const T &)> callback) FL_NOEXCEPT
Register success callback - returns reference for chaining.
Definition promise.h:115
Promise class that provides fluent .then() and .catch_() semantics This is a lightweight wrapper arou...
Definition promise.h:58
fl::task::Promise< Response > fetch_get(const fl::string &url, const FetchOptions &request)
HTTP GET request.

Await-based approach:

if (result.ok()) {
const fl::net::http::Response& response = result.value();
// Use response...
}
Result type for promise operations.
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

TO RUN THIS TUTORIAL:

For WASM (recommended for networking):

  1. Install FastLED: pip install fastled
  2. cd into this examples directory
  3. Run: fastled Client.ino
  4. Open the web page and check browser console for detailed fetch results

For other platforms: Uses mock responses for testing the API without network connectivity

Definition in file Client.ino.

#include "FastLED.h"
#include "ClientReal.h"
+ Include dependency graph for Client.ino:

Go to the source code of this file.