Javascript stops event

Is it possible to stop the event at all and make it finish?

string rows // Correctly formatted csv string.

csv
  .fromString(rows, { headers: false })
  .on('data', (data) => {
    if (condition) {
       // force to end.
    }
  }
  .on('end', () => cb(units));

      

So I try to break out of the csv file earlier.

+3


source to share


2 answers


You seem to be using node-csvtojson .

I think you can stop firing the data event like this:



const converter = csv.fromString(rows, { headers: false })
converter.on('data', (data) => {
  if (condition) {
    converter.removeAllListeners('data');// where 'data' is the name of the event. If called without any arguments, all listeners added to csv converter will be removed
  }
}

      

Source: See this thread on Github .

+2


source


How about this?



const task = csv.fromString(rows, { headers: false })
task.on('data', (data) => {
  if (condition) {
    task.on('end', () => cb(units));
  }
}

      

+1


source







All Articles