当前位置:网站首页>NodeJS(一) 事件驱动编程
NodeJS(一) 事件驱动编程
2022-04-23 06:27:00 【老少年】
Node.js使用大量事件,这也是为什么Node.js相对于其他类似技术比较快的原因之一。当Node启动其服务器,就可以简单地初始化其变量,声明函数,然后等待事件的发生。
虽然事件似乎类似于回调。不同之处在于当回调函数被调用异步函数返回结果,其中的事件处理工作在观察者模式。监听事件函数作为观察者。每当一个事件被解雇,其监听函数开始执行。Node.js有多个内置的事件。 主要扮演者是 EventEmitter,可使用以下语法导入。
var events = require('events');//添加事件模块的引用
//create an eventEmitter object创建事件发射器对象
var eventEmitter = new events.EventEmitter();
//create a function connected which is to be executed
//when 'connection' event occurs
//创建一个连接的要执行的函数
//“connection”事件发生时
var connected = function connected() {
console.log('connection succesful.');
// fire the data_received event 启动数据接收事件
eventEmitter.emit('data_received');
}
// bind the connection event with the connected function将连接事件与连接的函数绑定
eventEmitter.on('connection', connected);
// bind the data_received event with the anonymous function
eventEmitter.on('data_received', function(){
console.log('data received succesfully.');
});
// fire the connection event
eventEmitter.emit('connection');
console.log("Program Ended.");//
eventEmitter.on('connection', connected);将连接事件connection与 connected函数绑定,当执行eventEmitter.emit('connection');的时候触发事件执行与之对应的函数
版权声明
本文为[老少年]所创,转载请带上原文链接,感谢
https://blog.csdn.net/laoshaonian/article/details/101541261
边栏推荐
- SAP CR传输请求顺序、依赖检查
- 5. Sql99 standard: internal connection and external connection
- Reflection on the systematic design of Android audio and video caching mechanism
- 中间人环境mitmproxy搭建
- Learn to use search engines
- 'NPM' is not an internal or external command, nor is it a runnable program or batch file
- 颜色转换公式大全及转换表格(31种)
- Mvcc (multi version concurrency control)
- 手游的热更方案与动态更新策略
- 快速排序
猜你喜欢

h5本地存储数据sessionStorage、localStorage

keytool: command not found

Visualization Road (IX) detailed explanation of arrow class

利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)

SAP TRANSLATE使用数据对象掩码示例

SAP PI/PO Soap2Proxy 消费外部ws示例

SAP ECC连接SAP PI系统配置

SAP pi / PO rfc2soap publishes RFC interface as WS example

Configure NPM

SAP SALV14 后台输出SALV数据可直接保存文件,发送Email(带排序、超链接、筛选格式)
随机推荐
Apache Hudi 如何加速传统的批处理模式?
如何判断点是否在多边形内(包含复杂多边形或者多边形数量很多的情况)
移动布局(flex布局、视口标签)
Ogldev reading notes
4. Multi table query
ABAP 实现发布RESTful服务供外部调用示例
游戏辅助脚本开发之旅
3.排序语句
7. sub query
Install and configure Taobao image NPM (cnpm)
SAP 导出Excel文件打开显示:“xxx“的文件格式和扩展名不匹配。文件可能已损坏或不安全。除非您信任其来源,否则请勿打开。是否仍要打开它?
Processing of common dependency module
斐波拉去动态规划
H5 local storage data sessionstorage, localstorage
Reflect on the limitations of event bus and the design and implementation of communication mechanism in component development process
[self motivation series] what really hinders you?
C#使用拉依达准则(3σ准则)剔除异常数据(.Net剔除一组数据中的奇异值)
js之作用域、作用域链、全局变量和局部变量
[Ted series] how does a habit change my life
C# 多个矩形围成的多边形标注位置的问题