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

99
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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