我正在使用filewalker npm包来遍历文件。
一旦满足了特定的条件,我正试图通过退出流来限制在流中读取的文件数量。(例如流5个文件路径)而不是等待exit事件。(这是由于大量的文件,我想分页流)
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()
})是否有手动取消/退出流的方法?
发布于 2017-04-21 11:26:29
虽然这不是问题的答案,但我通过用filewalker替换walk来解决这个问题。它基本上是做同样的事情,除了它有next函数,它允许我控制程序是否会执行下一个。
const walk = require('walk')
let walker = walk.walk(dataDirPath, {followingLinks: false})
let counter = 0
walker.on('file', (root, stat, next) => {
console.log(root + '/' + stat.name);
counter++
if(counter < 1) next()
})https://stackoverflow.com/questions/43540958
复制相似问题