How can I detect that await was mentioned before calling my function?

Maybe some magic like this:

function x(){
   return new Promise((resolve, reject)=>{
       console.log(x.awaitWasCalled) // true  
   })
}
const a = await x()

I’m asking about that because I’ve seen Mongoose (a library) is able to detect weather you’ve called await or not, HERE
Here is what they’re saying:

Mixing promises and callbacks can lead to duplicate entries in arrays.
For example, the below code inserts 2 entries into the tags array,
*not just 1.

const BlogPost = mongoose.model('BlogPost', new Schema({
  title: String,
  tags: [String]
}));

// Because there's both `await` **and** a callback, this `updateOne()` executes twice
// and thus pushes the same string into `tags` twice.
const update = { $push: { tags: ['javascript'] } };
await BlogPost.updateOne({ title: 'Introduction to Promises' }, update, (err, res) => {
  console.log(res);
});

How Mongoose for example was able to detect that I’m using await or not?

116 thoughts on “How can I detect that await was mentioned before calling my function?”

Leave a Comment