How to make learnyounode #9 juggling async work

notthehoff

I am trying to go through nodeschool's learnyounode.

This problem is the same as the previous problem (HTTP COLLECT) in that you need to use http.get(). However, this time you will be provided with three URLs as the first three command-line arguments.

You must collect the complete content provided to you by each of the URLs and print it to the console (stdout). You don't need to print out the length, just the data as a String; one line per URL. The catch is that you must print them out in the same order as the URLs are provided to you as command-line arguments.

Here is my code:

var bl = require('bl');
var http = require('http');

var urls = [process.argv[2], process.argv[3], process.argv[4]]
var dataArray = [];
var count = 0;


for (var i = 0; i <= urls.length-1; i++) {
  http.get(urls[i], function(response){ 
    response.setEncoding('utf8').pipe(bl(function (err, data) { 
      dataArray[i] = data.toString();
      count++;
      if (count==2){
        dataArray.forEach(function(item){
          console.log(item);
        })
      }
    }));
  });

};

And when I try to verify it:

  1. ACTUAL: "She'll be right gyno mate flat out like a milk bar. Grab us a gutta also mad as a battler. "
  2. EXPECTED: "Lets get some parma to watch out for the pav. She'll be right slabs no worries he's got a massive cracker. Lets get some gobful flamin she'll be right thongs. "

  3. ACTUAL: ""

  4. EXPECTED: "Get a dog up ya ute with it'll be sickie. He's got a massive yobbo bloody as cross as a bonza. "

  5. ACTUAL:

  6. EXPECTED: "She'll be right gyno mate flat out like a milk bar. Grab us a gutta also mad as a battler. "

  7. ACTUAL:

  8. EXPECTED: ""

What am I doing wrong here?

bengrin

You want to check if count == 3 because you have 3 urls.

Also you don't want to use a function within a loop like that. In this case the value of i is 3 every time your anonymous function is executed ... That's why you're only outputting the expected value of the third url.

This is a great example of whats going on: JavaScript closure inside loops – simple practical example

By wrapping the function in a new function you can make sure that i is exactly what you want it to be.

This should work:

var bl = require('bl');
var http = require('http');

var urls = [process.argv[2], process.argv[3], process.argv[4]];
var dataArray = [];
var count = 0;


function juggle (i) {
  http.get(urls[i], function(response) {
    response.setEncoding('utf8').pipe(bl(function(err, data) {
      dataArray[i] = data.toString();
      count++;
      if (count == 3) {
        dataArray.forEach(function(item) {
          console.log(item);
        });
      }
    }));
  });
}

for (var i = 0; i < 3; i++) {
  juggle(i)
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

learnyounode #9 juggling async

From Dev

learnyounode #9 juggling async, can official solution break?

From Dev

learnyounode - async juggling - Duplicate results of http.get()

From Dev

Node.js "LearnYouNode" juggling async function not working

From Dev

How to make an Async API work?

From Dev

how to make a for loop work with a async.parallel()

From Dev

'Async Juggling' - What is it really asking me to do?

From Dev

How do I make this async foreach loop work with promises?

From Dev

How do I make this javascript async while loop work?

From Dev

How to make IE 9 work with Bootstrap Multiselect plugin by davidstutz?

From Dev

How to make IE 9 work with Bootstrap Multiselect plugin by davidstutz?

From Dev

'Juggling Async' - Why does my solution not return anything at all?

From Dev

How to make async with linq

From Dev

How to make HttpWebRequest async

From Dev

How to make ReactiveCommand Async ?

From Dev

How to make ReactiveCommand Async ?

From Dev

How to make a code async?

From Dev

Phone links are not working inside iframe though it does work in div in iOS9 web. how to make phone links make work in iOS9 safari?

From Dev

How to make kafka-python or pykafka work as an async producer with uwsgi and gevent?

From Dev

Node.js how to install learnyounode?

From Dev

What is the difference between these two solutions on learnyounode 9#?

From Dev

How can I make this CSS layout work in IE9 / 10?

From Dev

How does async work in Express?

From Dev

How to make Repository Methods Async?

From Dev

How to make a function run async

From Dev

How to make it async in javascript or node?

From Dev

How to make a async REST with Spring?

From Dev

How to make it async in javascript or node?

From Dev

How to make async call to DotNetCircuitBreaker

Related Related

  1. 1

    learnyounode #9 juggling async

  2. 2

    learnyounode #9 juggling async, can official solution break?

  3. 3

    learnyounode - async juggling - Duplicate results of http.get()

  4. 4

    Node.js "LearnYouNode" juggling async function not working

  5. 5

    How to make an Async API work?

  6. 6

    how to make a for loop work with a async.parallel()

  7. 7

    'Async Juggling' - What is it really asking me to do?

  8. 8

    How do I make this async foreach loop work with promises?

  9. 9

    How do I make this javascript async while loop work?

  10. 10

    How to make IE 9 work with Bootstrap Multiselect plugin by davidstutz?

  11. 11

    How to make IE 9 work with Bootstrap Multiselect plugin by davidstutz?

  12. 12

    'Juggling Async' - Why does my solution not return anything at all?

  13. 13

    How to make async with linq

  14. 14

    How to make HttpWebRequest async

  15. 15

    How to make ReactiveCommand Async ?

  16. 16

    How to make ReactiveCommand Async ?

  17. 17

    How to make a code async?

  18. 18

    Phone links are not working inside iframe though it does work in div in iOS9 web. how to make phone links make work in iOS9 safari?

  19. 19

    How to make kafka-python or pykafka work as an async producer with uwsgi and gevent?

  20. 20

    Node.js how to install learnyounode?

  21. 21

    What is the difference between these two solutions on learnyounode 9#?

  22. 22

    How can I make this CSS layout work in IE9 / 10?

  23. 23

    How does async work in Express?

  24. 24

    How to make Repository Methods Async?

  25. 25

    How to make a function run async

  26. 26

    How to make it async in javascript or node?

  27. 27

    How to make a async REST with Spring?

  28. 28

    How to make it async in javascript or node?

  29. 29

    How to make async call to DotNetCircuitBreaker

HotTag

Archive