# runIt

***runIt(function(){** // do something here with this **})***

* **The context object** is available as a receiver (*this*).
* **The return value** is the lambda result.

*runIt* does the same as *withIt* but it is implemented as an extension function. So like *letIt*, you can call it on the context object using dot notation.

*runIt* is useful when your lambda function both initializes objects and computes the return value.

```ts
const service = new MultiportService("https://api.example.com/data", 80)

const result = service.runIt(function () {
    this.port = 8080;
    const result = this.query(prepareRequest());
    console.debug(`Request sent to port ${this.port}"`);
    return result;
});

// the same code written with letIt() function:
const letResult = service.letIt(it => {
    it.port = 8080;
    const result = it.query(prepareRequest());
    console.debug(`Request sent to port ${it.port}"`);
    return result;
});
```

You can also invoke *runIt* as a non-extension function. The non-extension variant of *runIt* has no context object, but it still returns the lambda result. Non-extension run lets you execute a block of several statements where an expression is required. In code, non-extension *runIt* can be read as "**run the code block and compute the result.**"

```ts
const hexNumberRegex = runIt(() => {
    const digits = "0-9"
    const hexDigits = "A-Fa-f"
    const sign = "+-"

   return  new RegExp(`[${sign}]?[${digits}${hexDigits}]+`, "g");
});

let match;
while ((match = hexNumberRegex.exec("+123 -FFFF !%*& 88 XYZ")) !== null) {
    console.log(match[0]);
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://katxupa.gitbook.io/katxupa/get-a-taste-of-katxupa/usage/scope-functions/functions/runit.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
