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

Detailed Description

Educational tutorial for FastLED WASM networking 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::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:

EXPLICIT TYPE EXAMPLES:

Promise-based approach:

fl::promise<fl::response> promise = fl::fetch_get("http://example.com");
promise.then([](const fl::response& response) { /* handle success *&zwj;/ })
.catch_([](const fl::Error& error) { /* handle error *&zwj;/ });
promise & then(fl::function< void(const T &)> callback)
Register success callback - returns reference for chaining.
Definition promise.h:129
Promise class that provides fluent .then() and .catch_() semantics This is a lightweight wrapper arou...
Definition promise.h:72
HTTP response class (unified interface)
Definition fetch.h:83
fl::promise< response > fetch_get(const fl::string &url, const fetch_options &request)
HTTP GET request.
Definition fetch.cpp:180

Await-based approach:

fl::promise<fl::response> promise = fl::fetch_get("http://example.com");
if (result.ok()) {
const fl::response& response = result.value();
// Use response...
}
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

TO RUN THIS TUTORIAL:

For WASM (recommended for networking):

  1. Install FastLED: pip install fastled
  2. cd into this examples directory
  3. Run: fastled NetTest.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 NetTest.ino.

#include "fl/sketch_macros.h"
#include "platforms/sketch_fake.hpp"
+ Include dependency graph for NetTest.ino:

Go to the source code of this file.