【问题标题】:Gulp: How to create a stream from a promise of an array of streams?Gulp:如何从一组流的承诺中创建一个流?
【发布时间】:2015-11-11 15:58:57
【问题描述】:

我有一个由异步函数启动的任务列表。我最终得到了一系列流的承诺。我需要将这些流合并为一个流并将其返回给 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.apply(null, streams).pipe(dest)
  • @vp_arth dest 是一个可读(不可写)的流。

标签: node.js gulp event-stream


【解决方案1】:

es.merge 返回可读流。
没有理由再创建一个 es.readable
我有很长一段时间没有 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

【讨论】:

  • 这些都不产生流。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-07
  • 2020-06-04
  • 2020-12-05
  • 1970-01-01
相关资源
最近更新 更多