节点stream – 当stream完成时,使同步返回数组。
我写了这个函数从stream中提取文件列表。 它工作,但是stream是asynchronous的, files
数组在stream完成之前返回。 我真的不想使用承诺库…尽量保持代码轻。 stream完成后如何返回files
数组?
function fileList(source) { var files = []; source.pipe(through2.obj(function(obj, enc, next) { file = obj.history[0].split("/").pop(); files.push(file); next(); })); return files; }
提供一个callback,而不是从函数返回一个值:
function fileList(source, callback) { var files = []; source.pipe(through2.obj(function(obj, enc, next) { file = obj.history[0].split("/").pop(); files.push(file); next(); }, function(flushcb) { flushcb(); callback(null, files); })); } // ... fileList(stream, function(err, files) { if (err) throw err; // use `files` here ... });
- 在Ubuntu下,箭头函数不能在节点–harmony中工作
- 护照-socketio商店属性configuration
- Mongoose:如何在填充期间排除虚拟ID字段
- 多次asynchronous调用后callback?
- 在使用作业队列(kue / node / express)时将响应发送回请求者
- 在Node.js中将json数据读入全局variables
- ipython相当于javascript / coffeescript为node.js?
- (node:2157)DeprecationWarning:Mongoose:mpromise(mongoose的默认承诺库)已被弃用,请插入自己的承诺库
- 恢复属性键/值