Skip to content

Instantly share code, notes, and snippets.

@w7089
Created January 9, 2019 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w7089/25e8f665ab086a526395d4d057eff779 to your computer and use it in GitHub Desktop.
Save w7089/25e8f665ab086a526395d4d057eff779 to your computer and use it in GitHub Desktop.
async_exec_time.js
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