I've come across this piece of code:
const results = await Promise.all(
[ Model1.find({}), Model2.find({}) ],
Model3.find({})
),
v1 = results[0],
v2 = results[1],
v3 = results[2]
which is invoking all()
with an array and a single object — `Model* are Mongoose models.
This is an easily fixed bug, but I'd like to understand how it is giving the resulting values, which are:
- v1 holds all the documents corresponding to
Model1
- v2 holds all the documents corresponding to
Model2
- v3 is
undefined
As explained in this answer on the comma operator, I expected only the Model3.find({})
promise to actually return data in results
, as the comma operator should evaluate the first operand but return its second operand (to Promise.all()
). But it's instead the other way around: results[0]
and results[1]
both contain data, while results[2]
(and thus v3
) is undefined
.
What am I missing?