Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!