Asynchronous map with Highland.js

I have a Highland stream that periodically receives data from the server. I need to do a database search inside the map. I cannot find mention of doing anything asynchronous in any of the Highland converters.

+4
source share
2 answers

You can use consumeto process the stream asynchronously.

_([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]
});
+2
source

After use, .mapyou can use .sequenceas:

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);
});

The spell is here .

Or in parallel with .parallel:

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);
});

Spell here

+1
source

Source: https://habr.com/ru/post/1585077/


All Articles