Skip to content

Instantly share code, notes, and snippets.

@rgruesbeck
Created March 29, 2014 00:50
Show Gist options
  • Save rgruesbeck/9846199 to your computer and use it in GitHub Desktop.
Save rgruesbeck/9846199 to your computer and use it in GitHub Desktop.
var http = require('http');
var bl = require('bl');
//get data from url.
function curl(url) {
http.get(url, function (res) {
res.pipe(bl(function (err, data) {
if (err) throw err;
else {
return data;
}
}));
});
};
//convert to string and print
function log(thing) {
console.log(thing.toString());
};
//map this array of urls to the output of the curl function / then print each.
[process.argv[2], process.argv[3], process.argv[4]].map(curl).forEach(log);
@rgruesbeck
Copy link
Author

I'm trying to map the array of urls to an array of the outputs of the "curl" function...
Instead I get an array of undefined.

@hahla
Copy link

hahla commented Mar 29, 2014

maybe because your function log doesn't return anything?

@rgruesbeck
Copy link
Author

"log" prints to the console, its "curl" that returns undefined if that make any sense... I need to understand what happens when "return data;" happens.
Its not getting returned form "curl", but I don't know how to get it to.

@shayanjm
Copy link

You don't want to 'return' from an async function.
How to use return values:

var something = somethingElse(thing);

The value of 'something' is populated by the return value of somethingElse() with param thing. If somethingElse() takes forever to return, then your entire application will have to wait until 'something' has a value.

Instead of return data;, you should actually deal with your data there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment