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

166
Vistas
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 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