Search code examples
node.jsstreamhighland.js

Using HighlandJS streams with http requests


I want to take an array of urls and map it to an array of pageranks for each url.

I wanted to try it out with streams, but I'm not sure I understand what I'm doing.

I've tried several things, this is where I landed (not working):

app.js

var getPageRank = require('pagerank');
var _ = require('highland');

var urls = [
    'google.com',
    'yahoo.com',
    'bing.com'
];

// Example usage of getPageRank
getPageRank(urls[0], function(err, ranking) {
    console.log(ranking);
});

var getPR = _.wrapCallback(getPageRank);

_(urls).map(getPR).toArray(function (myAnswer) {
    console.log(myAnswer);
});

package.json

{
  "name": "page-rank",
  "version": "0.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "highland": "^2.5.1",
    "pagerank": "^2.0.0"
  }
}

The output appears to be an array of streams, which seems odd coming out of toArray() and google's page rank of 9. I can't figure out how to wrap getPageRank so that it waits to return the value in the callback.


Solution

  • Since you have stream of streams, you can use Stream.merge() or Stream.flatten() to merge values from each stream into a new stream:

    _(urls).map(getPR).merge().toArray(function (myAnswer) {
        console.log(myAnswer);
    });