Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why one works and other doesn't work to async juggling? #698

Open
muthuvenkat opened this issue Dec 26, 2019 · 1 comment
Open

Why one works and other doesn't work to async juggling? #698

muthuvenkat opened this issue Dec 26, 2019 · 1 comment

Comments

@muthuvenkat
Copy link

muthuvenkat commented Dec 26, 2019

This solution works

const http = require('http')
const concat = require('concat-stream')

var count = 0;
var content = [];
var result = [];
for( var i = 0 ; i  < 3 ; i++) {
  getData(i)
}

function getData(i) {
  http.get(process.argv[i+2],  (res) =>{
      res.setEncoding('utf8');
      res.on('error', console.error)
      res.pipe(concat(function (data) {
        pushData(data, i) }))
  }).on('error',handleError)

}


function pushData(data, i){
  result[i] = data;
  count = count+1;
  if(count == 3)
    printAll()

}

function printAll(){
result.forEach(function (file) {
  console.log(file)
})
}

function handleError(err) {
  console.error(err)
  process.exit(1)
}

Where as this one doesn't work

const http = require('http')
const concat = require('concat-stream')

var count = 0;
var content = [];
var result = [];
for( var i = 0 ; i  < 3 ; i++) {
  //getData(i)
//}

// function getData(i) {
  http.get(process.argv[i+2],  (res) =>{
      res.setEncoding('utf8');
      res.on('error', console.error)
      res.pipe(concat(function (data) {
        pushData(data, i) }))
  }).on('error',handleError)
}


function pushData(data, i){
  result[i] = data;
  count = count+1;
  if(count == 3)
    printAll()

}

function printAll(){
result.forEach(function (file) {
  console.log(file)
})
}

function handleError(err) {
  console.error(err)
  process.exit(1)
}

The only difference the method **getData(i) ** , why one works and other doesn't
any ideas ?

@muthuvenkat muthuvenkat changed the title Why one works and other doesn't work to async juggling work? Why one works and other doesn't work to async juggling? Dec 26, 2019
@StudioSpindle
Copy link

StudioSpindle commented Feb 29, 2020

I personally find the for loop confusing. Perhaps this will provide you with some clarity:

'use-strict';
const http = require('http')
const url1 = process.argv[2];
const url2 = process.argv[3];
const url3 = process.argv[4];

// see notes
getContents(url1, function () {
  getContents(url2, function () {
    getContents(url3);
  });
});


function getContents(url, callback) {
  http.get(url, function (res) {
    let rawData = "";
    res.setEncoding('utf8');
    res.on('error', function(err) {
      return console.log(err)
    })
    res.on('data', function(data) {
      rawData += data;
    });
    res.on('end', function() {
      console.log(rawData);
      callback ? callback() : null;
    })
  })
}

Notes:
The getContents function calls are synchronous. But I think that was the purpose of the exercise.

Also the getContents example are known as a callback hell. For a later stage probably something such as async await would be more useful. But for this exercise we actually

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants