当前位置:网站首页>Nodejs (I) event driven programming
Nodejs (I) event driven programming
2022-04-23 07:47:00 【youth who behaves like an adult】
Node.js Use a large number of events , That's why Node.js One of the reasons why it is faster than other similar technologies . When Node Start its server , You can simply initialize its variables , Declare functions , Wait for the event to happen .
Although the event seems to be similar to a callback . The difference is that the callback function returns the result asynchronously , Event handling works in observer mode . Listen to the event function as an observer . Whenever an event is dismissed , Its listening function starts to execute .Node.js There are multiple built-in Events . The main actors are EventEmitter, You can use the following syntax to import .
var events = require('events');// Add a reference to the event module
//create an eventEmitter object Create an event emitter object
var eventEmitter = new events.EventEmitter();
//create a function connected which is to be executed
//when 'connection' event occurs
// Create a connected function to execute
//“connection” Event time
var connected = function connected() {
console.log('connection succesful.');
// fire the data_received event Start data receiving event
eventEmitter.emit('data_received');
}
// bind the connection event with the connected function Bind the connection event to 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); Connect events connection And connected Function binding , When executed eventEmitter.emit('connection'); Trigger the event to execute the corresponding function
版权声明
本文为[youth who behaves like an adult]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230626191179.html
边栏推荐
- 利用网页表格导出EXCEL表格加线框及表格内部间距的问题
- RGB颜色转HEX进制与单位换算
- 系统与软件安全研究(二)
- FSM finite state machine
- 斐波拉去动态规划
- Mobile game performance optimization
- What are the total number of all courses of Mr. Tang Xiaoyang, who is very popular in CSDN (question mark)
- 保研准备经验贴——18届(2021年)中南计科推免到浙大工院
- ABAP 从CDS VIEW 发布OData Service示例
- MySQL8.0 安装/卸载 教程【Window10版】
猜你喜欢

Custom time format (yyyy-mm-dd HH: mm: SS week x)

Reflect on the limitations of event bus and the design and implementation of communication mechanism in component development process

SQL针对字符串型数字进行排序

Dropping Pixels for Adversarial Robustness

Redis connection error err auth < password > called without any password configured for the default user

防抖和节流

MySQL8.0 安装/卸载 教程【Window10版】

keytool: command not found

Design optimization of MySQL database

ABAP CDS VIEW WITH ASSOCIATION示例
随机推荐
Redis connection error err auth < password > called without any password configured for the default user
利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)
系统与软件安全研究(四)
SampleCameraFilter
Judge whether the beginning and end of the string contain target parameters: startswith() and endswith() methods
SAP PI/PO Soap2Proxy 消费外部ws示例
2. Restricted query
11. Table and library management
设置了body的最大宽度,但是为什么body的背景颜色还铺满整个页面?
简单理解==和equals,String为什么可以不用new
Apache Hudi 如何加速传统的批处理模式?
9. Common functions
Xamarin版的C# SVG路径解析器
解决在docker中部署mysql8, 密码正确但无法登陆MySQL问题
canvas学习第一篇
SAP PI/PO rfc2Soap 发布rfc接口为ws示例
[Ted series] how does a habit change my life
SAP 03-AMDP CDS Table Function using ‘WITH‘ Clause(Join子查询内容)
Dropping Pixels for Adversarial Robustness
NPM installation stepping pit