Tengo una función como esta,
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 } }Aunque estoy usando await para resolver la promesa antes de ir al siguiente paso , aún así registra PromiseProvider {} en el siguiente paso y la siguiente verificación de condición no se valida correctamente.
Tenga en cuenta que la misma función funciona bien a veces, pero no es consistente ya que enfrento el comportamiento mencionado anteriormente muchas veces, estoy usando el nodo 16 y aquí está la función getDetailsForTimestamp.
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); }); }); } };¿Por qué no usas simplemente
async await instead of PromiseIntente reemplazar su método "getDeatilsForTimestamp" con esto:
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; } } };