Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda