danmactough / node-feedparser

Robust RSS, Atom, and RDF feed parsing in Node.js
Other
1.97k stars 190 forks source link

[feature request] pass stream reference to callbacks #270

Closed wujekbogdan closed 5 years ago

wujekbogdan commented 5 years ago

Currently it's not possible to use arrow functions for callbacks because we have to access stream reference via this:

feedParser.on("readable", function() {
  const stream = this;
  // Do something
});

It would be great if the reference to stream was passed as an argument to the callback function, so that we could use arrow functions as follows:

feedParser.on("readable", (stream) => {
  // Do something
});
danmactough commented 5 years ago

@wujekbogdan Thanks for opening the issue.

If you want to use arrow functions, we don't need to pass the stream to the callback. You already will have a reference to the stream -- in your example, this is a reference to feedParser.

So, you could write:

feedParser.on("readable", (stream) => {
  let item;
  while (item = feedParser.read()) {
      if (argv.group) {
        items.push(item);
      }
      else {
        if (argv.json) {
          console.log(JSON.stringify(item));
        }
        else {
          console.log(util.inspect(item, null, 10, true));
        }
      }
    }
});