Previously, the queries were made in the following way:
Modelo.find({dato:'foo'},function(error, resultado){
});
However, the v4.9.4 documentation seems to me to be very confusing and shows that it should be done as follows:
let query = Modelo.find({dato:'foo'}).exec();
and then do the integration of the promises.
Is that the only correct way? Is it correct to continue using the old syntax? Is there any blog or document that shows a clearer summary of the documentation?
It is not the only correct way; no way. Using callbacks or promises is a matter of programmer culture and this is what will influence the use of one approach or another.
I personally recommend you to use promises. Why? For the simple reason it is the basis for new standards such as Async/Await and Import() (Extension
import
that returns a promise with the module, allowing us to import modules whenever we want). Legacy code is being migrated to ES6+ because of the great flexibility and possibilities it offers you to write better and more concise code.As I told you in the previous point, it is a matter of culture of the programmer. Callbacks will never go out of style because they are a mainstay of asynchronous code. You can use callbacks or use a more modern approach , such as promises and async/await.
Imagine you have three consecutive calls:
This is called callback hell and it is a very common problem that unfortunately makes the code very difficult to read, understand and even more difficult to maintain. Fortunately, promises help us most of all, semantically , they reduce the cognitive load of the code and it makes you write code in a somewhat more pleasant way.
With the advent of
async/await
, your code even looks like it's synchronous:Another example is the new proposal
Import()
:Whenever possible, use ES6+, even when you can't, use it and compile it to ES5 using https://babeljs.io.
PS: As of Node.js 7.8 it is already possible to use
async/await
natively.