Can someone explain this code snippet

I want to create a Captive Portal for one of my Projects. Normally i use the "standard" ESP8266Webserver.h but in this case i want to use the AsyncWebserver, because of the Async Feature for later purposes..., i read the documentation and try some examples and read the code but i dont understand the following snippet

server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
      request->send(200, "text/plain", String(ESP.getFreeHeap()));

The Basic structure is no problem...
"server" is my Object, "on" is my Method and "/", HTTP_GET, are "normal" parameters...

But what means: (AsyncWebServerRequest *request)

This is not the "normal" structure?! - There is no closing bracket at the end of parameter head like:
server.on(param,param, param**)**
It is open until behind the last curly bracket... is this a function inline the param?

And what means this: (AsyncWebServerRequest *request)
Why the - is this an Array with multiple Request pointer? and if so why are there only "normal" braces?

I hope someone can explain this code :slight_smile: It is part of this example in line 149...

Thanks for help ^^

This is called a lambda function.

When you check the ESPAsyncWebServer.h, you’ll find the function definition:

 AsyncCallbackWebHandler& on(const char* uri, WebRequestMethodComposite method, ArRequestHandlerFunction onRequest);

The third argument ‘ArRequestHandlerFunction’ is a typedef to std::function:

typedef std::function<void(AsyncWebServerRequest *request)> ArRequestHandlerFunction;

So you are essentially passing an “in-place” function to the webserver object.

i think the braces (and ";") are unnecessary and it boils down to the following which is send() returns some value that is cast pointer to "AsyncWebServerRequest" and the "" are a synonym (with distinctions)

server.on("/", HTTP_GET, 
      [](AsyncWebServerRequest *request) request->send(200, "text/plain", String(ESP.getFreeHeap()))