My co-worker and I were having a discussing and we were trying to figure this out. Assuming I have a NodeJS application connected to Mongo I am trying to reason about how this code will work. Here is a contrived example.
// #1 is an async request but it's not awaited and
// the promise is saved as a variable. because its
// an aggregate query it takes some time but i'm not
// concerned with the result just yet.
const promise = Model.find(aggregateQuery);
// #2 this a computationally heavy process
for (let item of await Model.find(query)) {
const result = dataManipulation(item);
const updates = await Model.findOneAndUpdate({ _id: item._id }, result);
}
// #3 ok, now I'm hoping Mongo has an answer for me
const answer = await promise();
I'm trying to reason about the behaviour of the promise. Will Mongo work on 1 and 2 using its threads? Does it matter whether or not #1 is await?