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

161
Vistas
Using await is not waiting for a Promise to resolve, can you please tell me why I'm facing this behavior?

I have got a function like this,

 const getCurrentDetails= async () => {
    const currentDateTime = new Date(moment('00:00','HH:mm')
                                    .tz('America/New_York')
                                    .toISOString());
    const currentDateDetail = await getDetailsForTimestamp(currentDateTime);
    console.log("currentDateDetail: ",currentDateDetail)  //prints PromiseProvider {}
  if(currentDateDetail){
  //do some stuff if details present
   }
}

Even though I'm using await to resolve the promise before going to the next step, but still it logs PromiseProvider {} in the next step and the following condition check fails to validate properly.

Note that the same function works fine sometimes, but not consistent as I face the above-mentioned behavior many times, I'm using node 16, and here's the getDetailsForTimestamp function.

const getDetailsForTimestamp = async (currentTimeStamp) => {
      const { db } = await database.getDb(process.env.DB_NAME);
      if (db) {
        return new Promise((resolve, reject) => {
          db.collection(process.env.COLLECTION_NAME)
            .findOne({ updatedAt: { $gte: currentTimeStamp } })
            .then((res) => {
              resolve(res);
            })
            .catch((err) => {
              reject(err);
            });
        });
      }
    };
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Why don't you just use

async await instead of Promise

Try with replacing your method "getDeatilsForTimestamp" with this:

const getDetailsForTimestamp = async currentTimeStamp => {
const { db } = await database.getDb(process.env.DB_NAME);
if (db) {
 try {
   return await db
    .collection(process.env.COLLECTION_NAME)
    .findOne({ updatedAt: { $gte: currentTimeStamp } });
  } catch (e) {
    return e;
  }
 }
};
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