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

121
Visualizações
how to access async return externally

im new to javascript and struggling with the concepts, how do i access the return value of the async function outside of it, i have tried a .then method but it only returns undefined... this is my code, i want to assign the result to the contractNumber variable outside of the async function so i can pass it to another function and also console.log the result externally... im coming over from learning python so im super confused

const inquirer = require('inquirer')


async function getContract() {
    await inquirer.prompt({
        type: 'input',
        name: 'retrieveContract',
        message: "Enter contract adress: ",
    })
    .then(answer => {
        console.log(`Targetting: ${answer.retrieveContract}`);
        results = answer.retrieveContract
        return results
    }); 
}

getContract()

contractNumber = results
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

    const inquirer = require('inquirer')

    async function getContract() {
        const result = await inquirer.prompt({
            type: 'input',
            name: 'retrieveContract',
            message: "Enter contract adress: "
        })

        console.log(`Targetting: ${result.retrieveContract}`);
        return result.retrieveContract;
    }

    contractNumber = await getContract();
about 4 years ago · Juan Pablo Isaza Relatório

0

To understand what's happening, await waits for the promise of inquirer.prompt to resolve into a return value, that you can then work with. There is no need for a .then clause, to work with the returned value.

There are two solutions:

  1. Use await, and simply continue writing your logic. (My preferred solution since its cleaner and more intuitive)
    async function getContract() {
        const answer = await inquirer.prompt({
            type: 'input',
            name: 'retrieveContract',
            message: "Enter contract adress: ",
        })
    
            console.log(`Targetting: ${answer.retrieveContract}`);
            results = answer.retrieveContract
            return results
     
    }
  1. Use .then, and pass in the callback function on .then
    async function getContract() {
        inquirer.prompt({
            type: 'input',
            name: 'retrieveContract',
            message: "Enter contract adress: ",
        }).then(answer => {
            console.log(`Targetting: ${answer.retrieveContract}`);
           results = answer.retrieveContract
           return results
         }); 
    }
about 4 years ago · Juan Pablo Isaza Relatório
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