The operator await
invokes the method then
and return to you the argument that would callback. It is semantic sugar to turn functional code into imperative code.
The equivalent of the first code without await
would be:
Question
.find({
$or: [
{ titulo: { $regex: termo, $options: 'i' } },
{ corpo: { $regex: termo, $options: 'i' } }
]
})
.count()
.then(questionscount => {
console.log(questionscount);
});
While the second would be:
Question
.find({
$or: [
{ titulo: { $regex: termo, $options: 'i' } },
{ corpo: { $regex: termo, $options: 'i' } }
]
})
.then(questions => {
console.log(questions.count());
});
As you can see, they are not equivalent. In the first code you create your query with two pipelines (find
and count
), while in the second you mount the query only with find
, and have it executed, and then invoke count
in the result, but the result does not have that method, the method belongs to the query, not to the result.
The equivalent would be to do:
var questionsQuery = Question.find({
$or: [
{ titulo: { $regex: termo, $options: 'i' } },
{ corpo: { $regex: termo, $options: 'i' } }
]
});
var questionsCount = await questionsQuery.count();
console.log(questionsCount);
What
Question.find
returns? An object?– Cmte Cardeal