0

I know this is 99% a very stupid question, but I'm just getting started with javascript and node.js.

Here is the code, I'll explain the problem later on:

function f(args){
    request.post({stuff}, 
    function optionalCallback(error, response, body) {
        if (error) {
            console.log('upload failed:', error);
        }
        
        console.log(JSON.parse(body)); // #2
        return JSON.parse(body);
        });
  }
  
  
// later on in the code, in a switch statement

case 'test':
    console.log(f(args));  // #1
    break;

Here is the issue I'm having: the console.log() #1 prints out undefined, while the console.log #2 prints out the expected output but after #1 (as in, half a second later)

#1 undefined  
#2 [object Object]

I know this is probably a very stupid mistake, but I've been losing my mind on it for hours now

The question is: why does it happen? How can I make sure it prints the object on both cases? (i.e. how do I wait for the function to finish before printing)

MathT
  • 65
  • 7

2 Answers2

2

Node works "asynchronously" unlike "synchronous" languages like C++, python . When you console.log your f(args) a separate event loop will start working with your function.

Think of it as a separate thread to "understand".

---{function call here}--->console.log #1 ( I called a function, it returned nothing)
            \
             \
          inside the function ---{post}---> 
                                    \
                                     \
                                      -----{response here}---->
                                                 \
                                                  \
                                                   -------> console.log #2 successfull

you want something like

---{function call here}-----------------{response here} -> console.log now                                  
                                              ^ 
            \                                  \
             \                                  \
          inside the function ---{post}--->      \ 
                                    \             \
                                     \             \
                                      -----{response here}---->
                                                 \
                                                  \
                                                   -------> console.log #2 successfull
Prasanna
  • 4,125
  • 18
  • 41
1

// later on in the code, in a switch statement

This is your first mistake ;)

The code in the switch statement is not executed "after" the function. It is the code that calls the function f(args).

Secondly, function f(args) does not return a value when it is called: the caller will therefore receive undefined.

The fact that there is a return statement within function f does not mean that the function itself returns a value, simply that the function (function f) will receive a value from the embedded optionalCallback function.

Sébastien
  • 11,860
  • 11
  • 58
  • 78