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.

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."

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]);
}

Last updated