Home > other >  Understanding function returns and callbacks
Understanding function returns and callbacks

Time:04-21

I am having an issue with the order of my functions, and I'm unsure why.

When hovering over the call for function_2 within the first function, VSCode gives me the result of Promise<void>, and the console outputs undefined when run (before function_2() even finishes).

Since I'm editing in VSCode, it tells me at the top when I hover over a return that it is for the request callback, so I have a general idea of what the issue is. Past that I have no idea, and it's most likely due to my lack of understanding about returns on promises and/or the true sequence of when things happen in a script.

Everything else in my code works without issue.

I have researched this quite extensively without luck (both on this site and in some general code documentation), but please let me know if there is something I missed.

I have included the general order of my functions below, and where the return calls are.

async function_1() {
const check = await function_2();
return console.log(check);
}
async function_2() {
...
    if(...){
    ...
        request(... => {
            ...
            while(...) {
                if(...) {
                ...
                return 'result'; //<<<RETURN
                }
                await  msg.channel.awaitMessages({
                ...
                }).then((c) => {
                    if() {
                    ...
                        if() {
                        ...
                        return 'result'; //<<<RETURN
                        };
                    } else if {
                    ...
                    return 'result'; //<<<RETURN
                    } else {
                    
                    };
                }).catch((c) => {
                ...
                return 'result'; //<<<RETURN
                });
            }; //<<<end 'while'
        }); //<<<end 'request'
    }; //<<<end 'if'
};

If my explanation was not clear, or more code is needed, please let me know.

CodePudding user response:

VSCode is able to analyze your code and conclude that it always returns Promise<void> a void return is the same as undefined in this context.

First of all Promise<T> is always returned from an async function.

Secondly, in the call to request(... => { you're providing a callback, the return value of this callback is used by request and not function_2.

This means that your returns inside your callbacks never return any value for function_2.

If you want to return a value from the parent function_2 you can resolve a custom Promise like this:

function function_2() { // note: not async because we already return a Promise
  return new Promise((resolve, reject) => {
    request(() => {
      /* ... */
      resolve("some value"); // effectively returns a value to the Promise
      if (error) {
        reject(error); // throw an error
      }
      /* ... */
    });

  });
}

try {
  const value = await function_2();
  console.log(value); // "some value"
} catch(e) {
  console.error(e);
}
  • Related