Malay
Malay

Reputation: 55

How can I pass a function in node js as a parameter to another function?

Please see the two blocks of code below. the first block of code is expected to perform the same way as the second block. But in the second block where function names are passed to other functions,it does not work and throws error. Note:-This is related to one of the modules in freecodecamp nodejs training.

FIRST BLOCK:-

request.pipe(map(function(chunk){return   chunk.toString().toUpperCase()})).pipe(response)

is different from SECOND BLOCK:-

request.pipe(transStream).pipe(response);
var transStream = map(data);

 function data (chunk)
  {
    return chunk.toString().toUpperCase();  
  }

the second block of code does not work while the first one does any help is greatly appreciated

Upvotes: 0

Views: 35

Answers (1)

Emilie_S4A
Emilie_S4A

Reputation: 36

I guess the code below should work:

var transStream = map(data);
request.pipe(transStream).pipe(response);
function data (chunk)
{
  return chunk.toString().toUpperCase();  
}

Upvotes: 1

Related Questions