# Can someone explain this code snippet

**URL:** <https://forum.arduino.cc/t/can-someone-explain-this-code-snippet/654069>\
**Category:** Programming\
**Created:** [May 10, 2020, 8:02am UTC](https://forum.arduino.cc/t/can-someone-explain-this-code-snippet/654069 "2020-05-10T08:02:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![j54j6](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@j54j6](https://forum.arduino.cc/u/j54j6)\
**Post date:** [May 10, 2020, 8:02am UTC](https://forum.arduino.cc/t/can-someone-explain-this-code-snippet/654069/1 "2020-05-10T08:02:18Z")

</div>

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](https://github.com/me-no-dev/ESPAsyncWebServer), 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

```cpp
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 🙂 It is part of [this](https://github.com/me-no-dev/ESPAsyncWebServer/blob/master/examples/ESP_AsyncFSBrowser/ESP_AsyncFSBrowser.ino) example in line 149...

Thanks for help ^^

---

<div class="post-metadata">

**Author:** ![LightuC](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lightuc/32/1128578_2.png) [@LightuC](https://forum.arduino.cc/u/LightuC)\
**Post date:** [May 10, 2020, 12:08pm UTC](https://forum.arduino.cc/t/can-someone-explain-this-code-snippet/654069/2 "2020-05-10T12:08:56Z")

</div>

This is called a lambda function.

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

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

```

The third argument 'ArRequestHandlerFunction' is a typedef to std::function:

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

```

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

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [May 10, 2020, 12:10pm UTC](https://forum.arduino.cc/t/can-someone-explain-this-code-snippet/654069/3 "2020-05-10T12:10:40Z")

</div>

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)

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

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:31am UTC](https://forum.arduino.cc/t/can-someone-explain-this-code-snippet/654069/4 "2021-05-06T00:31:00Z")

</div>


