当前位置:网站首页>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
边栏推荐
猜你喜欢

The page displays the current time in real time

SAP ECC连接SAP PI系统配置

H5 local storage data sessionstorage, localstorage

SAP pi / PO rfc2soap publishes RFC interface as WS example

Install and configure Taobao image NPM (cnpm)

keytool: command not found

SAP 导出Excel文件打开显示:“xxx“的文件格式和扩展名不匹配。文件可能已损坏或不安全。除非您信任其来源,否则请勿打开。是否仍要打开它?

ABAP 实现发布RESTful服务供外部调用示例

【NLP笔记】CRF原理初探

SAP PI/PO登录使用及基本功能简介
随机推荐
js之节点操作,为什么要学习节点操作
C# 文本文件的查找及替换(WinForm)
保研准备经验贴——18届(2021年)中南计科推免到浙大工院
js之什么是事件?事件三要素以及操作元素
Install and configure Taobao image NPM (cnpm)
[Ted series] how does a habit change my life
Design optimization of MySQL database
Preliminary configuration of OpenGL super Dictionary (freeglut, glew, gltools, GLUT)
typescript字典的使用
Mvcc (multi version concurrency control)
移动Web(字体图标、平面转换、颜色渐变)
3. Sort statement
How to judge whether a point is within a polygon (including complex polygons or a large number of polygons)
颜色转换公式大全及转换表格(31种)
js之函数的两种声明方式
unity 屏幕自适应
Super classic & Programming Guide (red and blue book) - Reading Notes
King glory - unity learning journey
NodeJS(一) 事件驱动编程
C#使用拉依达准则(3σ准则)剔除异常数据(.Net剔除一组数据中的奇异值)