1

I have a node module that watches for events.

sassWatcher
    .on('init', () => sasstoCSS(prod, config, sassPath))
    .on('update', () => sasstoCSS(prod, config, sassPath))

I'd make it one line. So it's like:

sassWatcher
        .on('init' || 'update', () => sasstoCSS(prod, config, sassPath))

Is there a way to do this in Node.js, regardless of the library? Putting the two events in an array doesn't seem to work.

Union find
  • 7,759
  • 13
  • 60
  • 111

1 Answers1

1

If you look at the docs for EventEmitter, you can see there is no built-in way of passing multiple event strings with a single handler.

https://nodejs.org/api/events.html

However, if you are just trying to stay DRY, there is a way you can refactor your code it is less repetitive.

['init', 'update'].forEach(event => {
    sassWatcher.on(event, () => sasstoCSS(prod, config, sassPath))
})
posit labs
  • 8,951
  • 4
  • 36
  • 66