Hacker News new | ask | show | jobs
by ricardobeat 1233 days ago
It's a bit out of fashion, but same thing can be achieved without any extra code using a built-in EventEmitter:

    let channel = new EventEmitter()
    await Promise.all([
      compile.browser(channel),
      compile.server(channel)
    ])

    // in compile.browser
    channel.emit("manifest", assetsManifest)

    // in compile.server
    channel.once("manifest", (assetsManifest) => ... )
A new emitter is used each time, so the end result is the same. Curious to hear what others think.
1 comments

EventEmitter doesn’t hold on to its values, so if the `channel.once` listener doesn’t get attached before `emit` is called, the value will be missed. Also, in order to wait on an event, you usually end up with a promise anyway (so `await` can be used).