Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
Understanding function returns and callbacks

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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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);
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda