Javascript 带Highland.js的异步映射

Javascript 带Highland.js的异步映射,javascript,stream,functional-programming,highland.js,Javascript,Stream,Functional Programming,Highland.js,我有一个Highland流,它定期从服务器获取数据。我需要在地图中查找数据库。我在任何Highland的Transformer中都找不到任何异步操作的内容。您可以使用异步方式处理流 _([1, 2, 3, 4]).consume(function(err, item, push, next) { // Do fancy async thing setImmediate(function() { // Push the number onto the new stream

我有一个Highland流,它定期从服务器获取数据。我需要在地图中查找数据库。我在任何Highland的Transformer中都找不到任何异步操作的内容。

您可以使用异步方式处理流

_([1, 2, 3, 4]).consume(function(err, item, push, next) {
  // Do fancy async thing
  setImmediate(function() {
    // Push the number onto the new stream
    push(null, item);

    // Consume the next item
    next();
  });
})).toArray(function(items) {
  console.log(items); // [1, 2, 3, 4]
});

使用
.map
后,可以使用
.sequence
作为:

var delay = _.wrapCallback(function delay(num, cb){
    setTimeout(function(){ cb(null, num+1); }, 1000);
});

_([1,2,3,4,5]).map(function(num){
    return delay(num);
}).sequence().toArray(function(arr){ // runs one by one here
    console.log("Got xs!", arr);
});

或与
并行
并行:

var delay = _.wrapCallback(function delay(num, cb){
    setTimeout(function(){ cb(null, num+1); }, 1000);
});

_([1,2,3,4,5]).map(function(num){
    console.log("got here", num);
    return delay(num);
}).parallel(10).toArray(function(arr){ // 10 at a time
    console.log("Got xs!", arr);
});