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

165
Visualizações
JS How to await an function from an object

Disclaimer: I am really tired and therefore my thinking ability isn't the greatest right now.

I want to write a code where I can choose that the program connects to a database or uses files for storage, etc.

I wish to make it so that my main program calls a function let's say DataHandler.read(users) and the rest of it is dealt with. (Another file which is imported deals with it.)

let dataHandler = {};

const databaseJS = require('./database');
dataHandler.connection = databaseJS.connect(database);
dataHandler.read = async function(data) {return await databaseJS.read(dataHandler.connection, data);};

// 1
let output = await dataHandler.read("SHOW DATABASES");
console.log(output);

// 2
dataHandler.read("SHOW DATABASES").then(output => {console.log(output);});

The 1.st thing is what I want, I would like to avoid using .then But only the second part works. If I remove the await from the first it gives back a promise, I dunno where else to put my await.

about 4 years ago · Santiago Gelvez
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