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

100
Views
Having trouble returning a promise and then consuming it after a database call

Im learning Javascript and im up to a section on Promises. So decided to try and convert some of my earlier projects to use Promises. But I cant get my head around it. Iv read a lot of guides and browsed through this site but I still cant understand why this isnt working.

This the function im working with, trying to do a database call that returns a promise, that can then be logged or whatever.

const getID = function (product) {
  let query = `select typeID from invTypes where UPPER(typeName) =UPPER('${product}')`;
  return new Promise((resolve, reject) => {
    resolve(
      DB.get(query, function (err, result) {
        if (err) {
          Bot.say(`Error looking up ${product}`);
        } else {
          console.log(result.typeID); //587
          return result.typeID;
        }
      })
    );
  });
};

getID(product).then((data) => console.log(data)); //Database {}

The console outputs

Database {}
587

so the .then(data => console.log(data)) //Database {} Is still happening before the promise is returned,

as you can see the other log from earlier up inside the function is actually logging after it.

Can someone help me with this iv been at this one function for hours now haha. I think I have some kind of fundamental misunderstanding that Im not picking up on from my bootcamp videos or anything im reading.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In promise code, you use resolve() in the asynchronous function's callback function to return values.

const getID = function(product) {
  let query = `select typeID from invTypes where UPPER(typeName) =UPPER('${product}')`;
  return new Promise((resolve, reject) => {
    DB.get(query, function(err, result) {
      if (err) {
        Bot.say(`Error looking up ${product}`);
        reject(`Error looking up ${product}`);
      } else {
        console.log(result.typeID); //587
        resolve(result.typeID);
      }
    })
  });
};

about 4 years ago · Juan Pablo Isaza Report
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!