首页 > 解决方案 > nodeJs中的箭头函数

问题描述

我在理解箭头函数时遇到问题 我知道箭头函数是这样键入的 ()=> 但我想知道我们在其中键入箭头函数的函数是如何工作的

喜欢

app.listen(3000 , () => console.log('foo'));

我想知道热听函数调用箭头函数?以及它如何调用没有任何名称的箭头函数

那么如果我想创建一个以箭头函数作为参数的函数,我该怎么做呢?

标签: javascriptnode.jsfunction

解决方案


这称为回调函数,查看 MDN 文档:https ://developer.mozilla.org/en-US/docs/Glossary/Callback_function

该函数在父函数的参数中命名。

function myFunc(callbackFunc) {
    //do stuff!
    console.log("in parent func");
    callbackFunc(); //calls the callback function passed as a param
    console.log("Callback done!"); //If there is async code in your callback function, this may happen BEFORE the callbackFunc() is finished. A common gotcha to watch out for.
}

myFunc(() => { console.log("Doing the callback") });

这里是 ExpressJS 如何使用回调函数:https ://expressjs.com/en/guide/using-middleware.html


推荐阅读