当我用命名函数替换内联函数时,函数调用失败

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

为什么运行下面的代码:

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

当此函数有名称时却没有:

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

someExpression.then(print(result)))

有错误:

ReferenceError: result is not defined

您没有在第二个示例中传递函数。您正在执行一个函数并传递其结果。

正确的做法是:

someExpression.then(print)