For instance, I’m writing a Mongoose utility method, and I’m wanting to return a value after the async methods resolve. This is a highly simplified example.
const testConnect = () => {
let msg;
mongoose.connect(mongoServer.getUri())
.then(() => {
msg ="Connection success!";
})
.catch((err) => {
msg = "Connection failure! Error: " + err;
});
return Promise.resolve(msg);
};
My test buildup utilizes this method in a matter like this.
beforeAll(() => {
iMDT.testConnect()
.then((result) => {
console.log(result);
})
.catch((err) => {
console.log(err);
});
});
Is there a simpler, more organic way to return that value as a resolved promise – without using the prototype Promise? It feels kludgy to me.
3
Answers
What you’re attempting to do won’t work because your asynchronous operation is non-blocking so you will do
return Promise.resolve(msg);
before there’s even a value inmsg
. It’s almost always a warning sign when you’re assigning higher scoped variables inside of a.then()
or.catch()
handler and then trying to use those variables at the top scope. That won’t work because of timing issues (attempting to use the variable before its value is set).Instead, you can just return the promise directly. The general advice is to use the promises you already have and not create new ones unnecessarily. Return the one you already have like this:
Note, how the failure option returns a rejected promise that contains a human readable message and also contains the original error object as the
cause
. This is the usual way to use promises, not to hide the rejection by returning a different resolved value. This allows the caller to more easily know if the operation succeeded or failed without having to compare to specific resolved values.And, this is also what your
beforeAll()
code block is expecting. It is expecting a rejected promise if the operation fails.The following example demonstrates an optimized code presented in a clear and concise manner. Please share some other approach if there is any even better answer, I’d like to gain some knowledge as well. Thank you.
I’m a big fan of async/await. Allows for transparent readability.