async_exec_time.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const EventEmitter = require('events') | |
class WithTime extends EventEmitter { | |
execute(asyncFunc, ...args) { | |
console.time('execute'); | |
this.emit('begin'); | |
asyncFunc(...args, (err, data) => { | |
if (err) { | |
return this.emit('error', err); | |
} | |
this.emit('data', data); | |
console.timeEnd('execute'); | |
this.emit('end') | |
}) | |
} | |
} | |
const withTime = new WithTime() | |
withTime.on('begin', () => console.log("about to exec")) | |
withTime.on('end', () => console.log('done with exec')) | |
withTime.on('data', (data) => console.log(data.length)) | |
withTime.on('data', (data) => console.log('hi' + data.length)) | |
withTime.prependListener('data', (data) => console.log('pre' + data.length)) | |
// withTime.on('error', (err) => console.log(err)) | |
// the same effect as onError listener | |
// to be invoked only once even that multiple exception pop | |
process.once('uncaughtException', (err) => { | |
// do some cleanup | |
console.log(err) | |
process.exit(1) | |
}) | |
process.on('uncaughtException', (err) => { | |
// do some cleanup | |
console.log(err) | |
process.exit(1) | |
}) | |
withTime.execute(fs.readFile, '') | |
withTime.execute(fs.readFile, __filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment