使用node.js和lazy时了解EOF
我有一个用 CoffeeScript 编写的例程,在 node.js 中运行,从 Jade 文件中读取行。它看起来像这样:
each_line = (file, callback) ->
last_line = null
lazy = Lazy(fs.createReadStream(file)).lines.map(String).filter (line) ->
not (
# Filter the not interesting rows in the top of the file
/^html$/i.test(line) or
/^\s+body$/i.test(line) or
/^\s+\/\/\s+Generator.*$/i.test(line)
)
lazy.forEach (line) ->
# We only emit those lines that are whole lines, multilines will become joined
if /^\t/g.test(line)
last_line += line.replace(/\t/g, '').replace(/(\r\n|\n|\r)/gm," ")
else
callback(last_line) if last_line
last_line = line.replace(/(\r\n|\n|\r)/gm," ")
由于所有这些都是异步运行的,所以我需要知道 EOF,以便我可以在整个迭代完成后执行操作。有人有什么建议吗?
I have a routine written in CoffeeScript, running in node.js that reads lines from a Jade-file. It looks like this:
each_line = (file, callback) ->
last_line = null
lazy = Lazy(fs.createReadStream(file)).lines.map(String).filter (line) ->
not (
# Filter the not interesting rows in the top of the file
/^html$/i.test(line) or
/^\s+body$/i.test(line) or
/^\s+\/\/\s+Generator.*$/i.test(line)
)
lazy.forEach (line) ->
# We only emit those lines that are whole lines, multilines will become joined
if /^\t/g.test(line)
last_line += line.replace(/\t/g, '').replace(/(\r\n|\n|\r)/gm," ")
else
callback(last_line) if last_line
last_line = line.replace(/(\r\n|\n|\r)/gm," ")
Since all this runs async, I need to know EOF so that I can perform operations after the whole iteration is done. Does anyone have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会先说我自己没有使用过 Lazy,但我只是看了文档。
我有点期待 Lazy 发出“结束”事件,但我无法判断是否会发出“结束”事件,无论哪种方式,您都可以将回调直接绑定到读取流上的结束事件。
I will preface this by saying that I haven't used Lazy myself, but I just looked at the docs.
I was somewhat expecting Lazy to emit an 'end' event, but I can't tell if it does, either way you can bind a callback directly to the end event on the read stream.
我相信 Lazy 会在创建的原始 Lazy 对象上发出“结束”事件:
I believe Lazy will emit an 'end' event on the original Lazy object that was created: