第一次调用 execute 将会触发 error 事宜,因为没有处理 error ,Node 法度榜样随之崩溃:
- events.js:163
- throw er; // Unhandled 'error' event
- ^
- Error: ENOENT: no such file or directory, open ''
- class WithTime extends EventEmitter {
- async execute(asyncFunc, ...args) {
- this.emit('begin');
- try {
- console.time('execute');
- const data = await asyncFunc(...args);
- this.emit('data', data);
- console.timeEnd('execute');
- this.emit('end');
- } catch(err) {
- this.emit('error', err);
- }
- }
- }
第二次履行调用将受到此崩溃的影响,并且可能根本不会被履行。
如不雅我们为这个 error 事宜注册一个监听器函数来处理 error,结不雅将大年夜不雷同:
- withTime.on('error', (err) => {
- // do something with err, for example log it somewhere
- console.log(err)
- });
如不雅我们履行上述操作,将会申报第一次履行 execute 时发送的缺点,然则此次 node 过程不会崩溃退出,其他法度榜样的调用也都能正常完成:
- { Error: ENOENT: no such file or directory, open '' errno: -2, code: 'ENOENT', syscall: 'open', path: '' }
- execute: 4.276ms
须要留意的是,基于 Promise 的函数有些不合,它们临时只是输出一个警告:
data 事宜被触发时会携带一个 data 对象。
- 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
推荐阅读
市纪委经由过程对大年夜数据云与干部填报的家庭房产等情况的量化数据比对,敏捷排查出漏报瞒报人员,进而大年夜干部工作、生活相干事项着手,抓早抓小、大年夜严监管,教导抢救了大年夜批干部,起到了强大年夜的教导>>>详细阅读
本文标题:[译]理解Node.js事件驱动机制
地址:http://www.17bianji.com/lsqh/36743.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示