Home > Software engineering >  Calculating parameter in function call
Calculating parameter in function call

Time:09-19

I understand JS lambda functions (mostly when I google JS lambda and JS =>) as below:

const var = (a,b) => { return a*b } or const var = (a,b) => a*b const var = () => a*b It is easy to explain that we assign value to variable by "calculating" anonymous function with (a,b) parameters or none.

But I was unable to google my question because it was always returning general lambda format as above. My question is how to read this code:

let printJobId = null;
let contentInProgress = false; 
async function instantPrint(content) {
    await print(
      content,
      v => (printJobId = v),
      v => {
        if (v) contentInProgress = content;
        else contentInProgress = null;
      },
      doneResult => finishPrint(doneResult.status)
    ).then(result => {
      notify(result);
    });
  }
  1. What exactly line v => (printJobId = v) does? It seems for me that it assigns v value to printJobId but why the hell we are placing it inside print function call, it doesnt have return value for the v on the left side.

By saying function call I mean functionName(param1, param2) so for me it have no sense at all.

  1. What exactly those lines does?
v => {
        if (v) contentInProgress = content;
        else contentInProgress = null;
      }

It's similar to the first question. No return from the curly brackets {}. So the v for me looks like it is void and only do some business logic which should not be inside function call as a one of the parameters.

The line

doneResult => finishPrint(doneResult.status)

is easy to understand as finishPrint will return some value so we pass to the print() function, doneResult variable which will have value of the finishPrint return.

I have added also svelte tag as this code might be connected with it, but I believe it is only JS question.

Thank you in advance for all your time!

CodePudding user response:

  1. Considering such a piece of code, we have to know callback definition. According to the MDN docs (https://developer.mozilla.org/en-US/docs/Glossary/Callback_function):

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

Both examples are such a type of the function - they should perform some actions when we are expected to be triggered in specific situations during executing outer function. Now, we have to focus on the print function. Imagine we had a server which has to hold information about previously performed action, for example to give an user ability to undo it. Then, we want to persist ID of the last action, but we don't want to strictly specify how it should be saved, only that we want to do it. To provide such a behaviour, we create print function with the callback as a parameter, defining what should be done here - assigning ID value to the consuming variable. To define how it should be done, we have to provide proper implementation of the assignment - here, we are assigning v value (which may be just random unique number) to the global variable storing last action's ID - printJobId.

  1. In this case, v could be a value changed periodically when executing some complex computations in the print function. That callback would be called every second and would assign value informing about processing state (for example, numeric value between 0 and 100 indicating processing status described in percentage).

CodePudding user response:

print is getting passed functions as arguments which are then called internally (a term for that is "callback"). Most often this is used to react some kind of event.

The value of an assignment is the assigned value, so the first bit of code (v => (printJobId = v), pulls out the variable into the local scope by assigning printJobId (for whatever reason, at some point or multiple points in the print process).

Arrow functions (that is what these are called) do not have to have a return value, so the second function simply has some side effects. v has to be a parameter because it is provided by whatever is calling the function inside print.

Looking at the structure, the first callback is probably called at the beginning of a process, the second at some kind of checkpoint and the last one at the end.

  • Related