Friday 21 April 2017

Exit stream manually before `on('exit')` event is reached

I am using filewalker npm package to walk through files.

I am trying to limit amount of files that would be read in stream by exiting stream once a specific condition is met. (e.g. streamed 5 file paths) rather than waiting exit event. (This is due to huge amount of files and I want to paginate stream)

  getFilePath: (dirPath, fileMatchExpression) => {
    return new Promise((resolve, reject) => {
      filewalker(dirPath)
      .on('file', filePath => {
        if (filePath.match(fileMatchExpression)){
          resolve(filePath)
          // how to force exit on this line?
        }
      })
      .on('error', err => reject(err))
      .on('done', _ => console.log('DONE!!'))
      .walk()
    })

Is there a way to cancel/exit stream by manually?



via Kunok

No comments:

Post a Comment