javascript - 'Juggling Async' - Why does my solution not return anything at all? -
javascript - 'Juggling Async' - Why does my solution not return anything at all? -
after asking question , getting helpful reply on 'async juggling' assignment in learnyounode
asking me do, set out implement myself.
the problem is, setup isn't having success! though i've referred other solutions out there, setup isn't returning results when learnyounode verify myscript.js
.
gist: jugglingasync.js
var http = require('http'); var app = (function () { // private variables... var responsesremaining, urls = [], responses = []; var displayresponses = function() { for(var iterator in responses) { console.log(responses[iterator]); } }; // public scope... var pub = {}; pub.main = function (args) { responsesremaining = args.length - 2; // every argument, force url , prep response. for(var = 2; < args.length; i++) { urls.push(args[i]); responses.push(''); } // every url, set off async request. for(var iterator in urls) { var = iterator; var url = urls[i]; http.get(url, function(response) { response.setencoding('utf8'); response.on('data', function(data) { if(response.headers.host == url) responses[i] += data; }); response.on('end', function() { if(--responsesremaining == 0) displayresponses(); }); }); } }; homecoming pub; })(); app.main(process.argv);
question: doing wrong?
this line
for(var iterator in urls) {
doesn't think does. loops on properties of urls
(see https://developer.mozilla.org/en-us/docs/web/javascript/reference/statements/for...in). instead, have like
for(var = 0; < urls.length; i++) { var url = urls[i]; ... }
or
urls.foreach(function(url, index) { ... });
javascript node.js asynchronous
Comments
Post a Comment