highland: Returning Promises from Highland methods

My workflow looks somewhat like this:

_(itemStream)
    .map(function(item){ return doAsyncThing(item); })
    .each(function(item){ console.log(item); });

… where doAsyncThing returns a Bluebird promise. I had expected for Highland to await resolution of the promise and pass the final resolution value to the .each call (much like Bluebird’s own .map and .each), but instead my .each method is receiving the Promise objects themselves.

Is it possible to make Highland behave like I’m trying to accomplish, without having to manually .then in the .each handler, as that would defeat the point of doing things this way?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 30

Most upvoted comments

You can use flatMap and wrap the promise in a stream:

_(itemStream)
    .flatMap(function(item){ return _(doAsyncThing(item)); })
    .each(function(item){ console.log(item); });

Ha. Your answer is actually better because it explains why it works.