javascript - nodeJs中的箭头函数
问题描述
我在理解箭头函数时遇到问题 我知道箭头函数是这样键入的 ()=> 但我想知道我们在其中键入箭头函数的函数是如何工作的
喜欢
app.listen(3000 , () => console.log('foo'));
我想知道热听函数调用箭头函数?以及它如何调用没有任何名称的箭头函数
那么如果我想创建一个以箭头函数作为参数的函数,我该怎么做呢?
解决方案
这称为回调函数,查看 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
推荐阅读
- heroku - 部署时Python套接字服务器不工作
- flutter - 如果在 Flutter android 中使用 webview 页面时互联网连接丢失,则显示消息“无互联网”
- cordova - 如何在从 PC 转移的 Mac 上运行 Ionic 应用程序
- javascript - 如何从对象中删除属性但保留其值?
- android - 删除图像会留下甚至不存在的空占位符 (Android Studio)
- numpy - 在 Spyder 的变量资源管理器中加载整个列表
- git - 在推送到 github 之前,我是否需要 git checkout 从 svn 转换创建的远程分支
- google-maps - form.currentstate.validate() 失败 - Flutter
- ios - 在动态 UITableView 中使用多个 UIPickerView
- node.js - 在使用 node 和 puppeteer 获得页面响应后,如何找到某个是否存在?