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

155
Vistas
how to obtain return value associated with a promise

I have a javascript application with a file called index.js as follows:

module.exports.func = async (obj) => {
    return {
      statusCode: 200,
      body: JSON.stringify({
        message: 'Success!',
        input: obj,
      }),
    };
};

To test this, I have written another file called index.test.js, which has the following code (it's not yet complete but has the code to illustrate my problem):

const exportedModule = require('./index');

test('Successfully execute func when empty object passed', () => {
  const obj = {};
  const expectedReturn = {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Success!',
      input: obj,
    }),
  };
  const actualReturn = exportedModule.func(obj);
  console.log(actualReturn);
  expect(actualReturn).toBe(expectedReturn);
});

When this code runs, the console.log(actualReturn) function is outputting Promise { <pending> }. Normally I would address this by adding an await before exportedModule.func, but for some reason this does not work and an error is thrown saying exportedModule.func is not asynchronous (even though I have used async when writing the function). Would anyone know of the correct way to get the actual returned value of the fucntion, and not a promise? Or is there something wrong with the way I'm getting the func function that's causing it to not be recognised as asynchronous?

about 4 years ago · Juan Pablo Isaza
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