How does mongoose's find() function hold on to its execution and wait for a later function in its chain to complete first? How does mongoose's find() function hold on to its execution and wait for a later function in its chain to complete first? mongoose mongoose

How does mongoose's find() function hold on to its execution and wait for a later function in its chain to complete first?


The find() function in mongoose calls the exec() function in the Query object to complete the query and return the result, like in the line of this file.

Actually no, it doesn't, at least not in your case. See the comment from three lines above:

// if we don't have a callback, then just return the query object

The .exec() method is only called by find(…, callback) if you pass a callback. When using promises, you don't.

Instead, the exec method is called by the then() method of the query which makes the query thenable, and which gets used when you await the query object.