1

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);
            });
        });
      }
    };
prashanthh
  • 21
  • 4
  • could you add `console.log(res)` above this line `resolve(res);`? what does it print? maybe your `res` is a promise – Georgy Mar 16 '22 at 11:50
  • 1
    [What is the explicit promise construction antipattern and how do I avoid it?](https://stackoverflow.com/q/23803743) – VLAZ Mar 16 '22 at 11:55
  • to be honest, I'm surprised the code runs at all since you are trying to use `await` in a function that **isn't** `async` - or am I missing something? – Bravo Mar 16 '22 at 12:20
  • It does not run. Also this `function getCurrentDetails()=>{` arrow-non-arrow-function is syntactically incorrect – FZs Mar 16 '22 at 12:26
  • 1
    clearly the issue is something else entirely :p the REAL code is probably doing things wrong in a different way – Bravo Mar 16 '22 at 12:27
  • Hi @Bravo and FZs, thanks for responding. It was a typing mistake, I've updated the correct code in the question. I still face the issue with syntactically correct code. – prashanthh Mar 16 '22 at 13:23

1 Answers1

1

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;
  }
 }
};
Usama Majid
  • 1,103
  • 13
  • 13
  • 2
    why do so many people `return await somethingThatReturnsAPromise` .... just `return somethingThatReturnsAPromise` and get on with your life :p – Bravo Mar 16 '22 at 12:22
  • and if you want to concentrate on the function that could not cause the issue - I'd write it like `const getDetailsForTimestamp = async($gte) => (await database.getDb(process.env.DB_NAME))?.collection(process.env.COLLECTION_NAME).findOne({updatedAt: {$gte}});` because I'm an idiot that likes one liners :p – Bravo Mar 16 '22 at 12:26