Skip to content

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

@muthuvenkat

Description

@muthuvenkat

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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions