Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda