我有一个由异步函数启动的任务列表。我最终得到了一系列流的 promise 。我需要将这些流合并为一个流并将其返回给 Gulp。以下是我的想法。这有点笨重。有更好的方法吗?
return es.readable(function(count, callback) {
var dest = this;
promise.then(function(streams) {
es.merge.apply(null, streams)
.on('end', dest.emit.bind(dest, 'end'))
.on('data', dest.emit.bind(dest, 'data'))
.on('error', dest.emit.bind(dest, 'error'));
});
});
最佳答案
es.merge
返回可读流。
没有理由再用 es.read
创建一个。
我已经很长时间没有 Node.js 经验了,但我认为我写的东西有帮助:)
// async way
function merged(promise, next) {
promise.then(function(streams) {
next(null, es.merge.apply(null, streams));
}, next);
}
// like a promise
function merged(promise) {
return {
then: function(ok, fail) {
promise.then(function(streams) {
ok(es.merge.apply(null, streams));
}, fail);
}
};
}
用法:
merged(promise, function(err, stream){stream.pipe(anyWritable);}); // async
merged(promise).then(function(stream){stream.pipe(anyWritable);}); // then
关于node.js - Gulp:如何从流数组的 promise 创建流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33654551/