代码之家  ›  专栏  ›  技术社区  ›  Union find

将多种侦听方法组合到一个node.js中

  •  1
  • Union find  · 技术社区  · 8 年前

    我有一个节点模块来监视事件。

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

    我会排成一行。就像是:

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

    在node.js中有没有一种方法可以做到这一点,而不管库是什么?将这两个事件放在一个数组中似乎不起作用。

    1 回复  |  直到 8 年前
        1
  •  1
  •   posit labs    8 年前

    如果你看医生 EventEmitter 您可以看到,没有内置的方法可以用单个处理程序传递多个事件字符串。

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

    但是,如果你只是想留下来 DRY ,有一种方法可以重构代码,它的重复性更低。

    ['init', 'update'].forEach(event => {
        sassWatcher.on(event, () => sasstoCSS(prod, config, sassPath))
    })