I was going through the mongoose docs when I Stumbled upon the line saying
Mongoose queries are not promises. They have a .then() function for co and async/await as a convenience. If you need a fully-fledged promise, use the .exec() function.
With this example
var query = Band.findOne({name: "Guns N' Roses"});
assert.ok(!(query instanceof Promise));
// A query is not a fully-fledged promise, but it does have a `.then()`.
query.then(function (doc) {
// use doc
});
// `.exec()` gives you a fully-fledged promise
var promise = query.exec();
assert.ok(promise instanceof Promise);
promise.then(function (doc) {
// use doc
});
Now, I didn't get what they meant when they said fully-fledge promise, like for me .then()
should be a promoise and then it also allows async and await.
So can someone please explain me what does fully-fledge promise mean?
Reference link: https://mongoosejs.com/docs/promises.html#queries-are-not-promises
Built-in Promises This means that you can do things like MyModel. findOne({}). then() and await MyModel. findOne({}). exec() if you're using async/await.
“promise” is an object or function with a then method whose behavior conforms to this specification. “thenable” is an object or function that defines a then method.
Async/await lets us write asynchronous code as if it were synchronous. This is especially helpful for avoiding callback hell when executing multiple async operations in sequence--a common scenario when working with Mongoose.
That means that the values returned by queries are thenables per the definition of the Promises/A+ spec, but not actual Promise
instances. That means they may not have all of the features of promises (for instance, catch
and finally
methods). Actual Promise
instances would be "fully-fledged" promises.
The English term "fully-fledged" means "complete" or "fully developed." It comes from ornithology (or at least, terminology related to birds): A chick (a young bird) that has its adult feathers is "fledged;" if it has all its adult feathers completely covering its down undercoat, it's fully-fledged.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With