[SOLVED] Calculating parameter in function call


This Content is from Stack Overflow. Question asked by Joe D.

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

const var = (a,b) => { return a*b }
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(
      v => (printJobId = v),
      v => {
        if (v) contentInProgress = content;
        else contentInProgress = null;
      doneResult => finishPrint(doneResult.status)
    ).then(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!


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.

This Question was asked in StackOverflow by Joe D. and Answered by H.B. It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?