AKA a “fake” promise vs a “real” promise — explained in the context of Mongoose Queries
I was happily doing some backend coding and at the part where I had to write a mongoose query to return a list of items, I got confused at whether I should chain .exec()
or .then()
behind the myModel.find()
method. So I ended up searching their docs in an attempt to (try to) figure out which is the one I should use. Here’s a paragraph from their documentation:
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.
At that moment I was like…what’s a “fully-fledged promise”? What does it even mean in this context?
And the journey of trying to figure out what that phrase means got me to this link and I found out that what they meant was that Mongoose queries are thenables and not promises. According to that link,
“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.
Okay so naturally, the question now is — what does “whose behavior conforms to this specification” mean? To answer this question, let us take a closer look at what a “real” A.K.A “fully-fledged promise” means (and then we’ll look at why it’s different from thenables after that).
#programming #web-development #javascript