Search code examples
javascriptfunction-calls

Function call fails when I replace an inline function with a named one


Why does the following code runs:

someExpression.then((result)=>{
    console.log(util.inspect(result,{depth:null}));
    return result;
}))

and when this function has a name it doesn't:

function print(result) {
    console.log(util.inspect(result,{depth:null}));
    return result;
}

someExpression.then(print(result)))

with error:

ReferenceError: result is not defined


Solution

  • You're not passing a function in your second example. You're executing a function and passing its result.

    The proper way to do this would be:

    someExpression.then(print)