Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

javascript - How Mongoose is able to know that I've used await on invoking their function or not? [Edited]

[Old question] How can you know if your function concumer used await on invoking your function or not:

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?

[EDIT] ******* After sometime, I've noticed that the answers bellow doesn't actually answer my question.

Please before answering my question: please read their comment above, they're saying: " // Because there's both await and a callback, this updateOne() executes twice"

this means: if you didn't use await, and you passed in a callback, this code will be invoked once, BUT if you used await + callback, this code will be invoked twice, therefore: the question is: how they're able to know if I've used await or not?!

Again: This means, if you didn't use await, this is going to invoke once, and if you used await, this is going to get invoked twice.

question from:https://stackoverflow.com/questions/65939288/how-mongoose-is-able-to-know-that-ive-used-await-on-invoking-their-function-or

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

You can't. In fact, the function is called before await "kicks in". These two are the same:

await myFunction();

let result = myFunction();
await result;

await is not a different way to call a function, it simply operates on Promises, which of course are often returned by functions. I.e. it operates on the return value of a function.

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

Are they actually able to detect that? Nothing in the documentation you linked seems to indicate that.

What they (and you) could do is checking whether a callback is passed or not. If one is passed then they shouldn't return a Promise and vice versa. But I guess that's not how they want to design the API.


This means, if you didn't use await, this is going to invoke once, and if you used await, this is going to get invoked twice.

Yes, but not because they detect anything but because await does something with the return value of the function. That's just how await works. So obviously, if you don't use await then the thing that await would do won't happen ;)

Here is an example:

function someFunction(value, callback) {
  // Nothing in here does any "await" detection
  let array = [];
  if (callback) {
    array.push(value);
    callback(array);
  }
  return {
    then(resolver) {
      array.push(value);
      resolver(array);
    }
  }
}

(async function() {
  console.log('with await', await someFunction(42, () => {}));
  someFunction(42, array => console.log('without await', array));
}());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...