譯 理解 NodeJS 中基於事件驅動的架構

2021-09-23 01:22:12 字數 4397 閱讀 9266

本文講的是[譯] 理解 nodejs 中基於事件驅動的架構,

譯文出自:掘金翻譯計畫

譯者:劉德元

薛丁格的貓

校對者:bambooom

zaraguo

function

filesize (filename, cb)

fs.stat(filename, (err, stats) => // 非同步

cb(null, stats.size); // 非同步

});}

const readfileasarray = function(file, cb) 

const lines = data.tostring().trim().split('\n');

cb(null, lines);

});};

10

1112

1314

15

readfileasarray('./numbers.txt', (err, lines) => );
readfileasarray('./numbers.txt')

.then(lines => )

.catch(console.error);

const readfileasarray = function(file, cb = () => {}) 

const lines = data.tostring().trim().split('\n');

resolve(lines);

cb(null, lines);

});});

};

resolve函式的預設值,在 promise 中,我們只要寫乙個空函式即可,例如() => {}.

async

function

countodd () catch(err)

}countodd();

註冊和取消註冊監聽函式

class

myemitter

extends

eventemitter

const myemitter = new myemitter();
const eventemitter = require('events');

class

withlog

extends

eventemitter

}const withlog = new withlog();

withlog.on('begin', () => console.log('about to execute'));

withlog.on('end', () => console.log('done with execute'));

withlog.execute(() =>

console.log('*** executing task ***'));

before executing

about to execute

*** executing task ***

done with execute

after executing

命名為 begin 的事件 emit 輸出了 「about to execute」;

內含方法的執行輸出了「*** executing task ***」;

另乙個命名事件輸出「done with execute」;

最後「after executing」。

// ...

withlog.execute(() => );

});

before executing

about to execute

done with execute

after executing

*** executing task ***

const fs = require('fs');

const eventemitter = require('events');

class

withtime

extends

eventemitter

this.emit('data', data);

console.timeend('execute');

this.emit('end');

});}}

const withtime = new withtime();

withtime.on('begin', () => console.log('about to execute'));

withtime.on('end', () => console.log('done with execute'));

withtime.execute(fs.readfile, __filename);

about to execute

execute: 4.507ms

done with execute

class

withtime

extends

eventemitter

catch(err)

}}

this.emit('error', err);
this.emit('data', data);
withtime.on('data', (data) => );
class

withtime

extends

eventemitter

console.timeend('execute');

});}}

const withtime = new withtime();

withtime.execute(fs.readfile, ''); // bad call

withtime.execute(fs.readfile, __filename);

events.js:163

throw er; // unhandled 'error' event

^error: enoent: no such file or directory, open ''

withtime.on('error', (err) => );

execute: 4.276ms

unhandledpromiserejectionwarning: unhandled promise rejection (rejection id: 1): error: enoent: no such file or directory, open ''

deprecationwarning: unhandled promise rejections are deprecated. in the future, promise rejections that are not handled will terminate the node.js process with a non-zero exit code.

process.on('uncaughtexception', (err) => );
withtime.on('data', (data) => `);

});withtime.on('data', (data) => `);

});withtime.execute(fs.readfile, __filename);

withtime.on('data', (data) => `);

});withtime.prependlistener('data', (data) => `);

});withtime.execute(fs.readfile, __filename);

原文發布時間為:2023年6月07日

nodejs基於事件通訊

話說nodejs是非同步呼叫的,所以無法用return返回結果。有兩種解決方式 callback函式和事件。兩種方式相比起來事件的方式更加簡潔。nodejs裡的事件主要使用它的events模組,繼承eventemitter。這裡寫了乙個monitorevent類 var util require u...

nodejs的事件驅動理解

引入 events 模組 var events require events 建立 eventemitter 物件 var eventemitter new events.eventemitter 建立監聽例項 繫結事件及事件的處理程式 eventemitter.on eventname event...

nodejs中的模組的理解

nodejs所謂的模組就是乙個檔案,或者是匿名函式。commonjs require exports module.exports。複製 364行列 module.prototype.require function path 複製 module.load function request,pare...