Sometimes, we want to make a promise return the resolved value instead of [object Promise] with JavaScript.
In this article, we’ll look at how to make a promise return the resolved value instead of [object Promise] with JavaScript.
How to make a promise return the resolved value instead of [object Promise] with JavaScript?
To make a promise return the resolved value instead of [object Promise] with JavaScript, we can use the await
keyword.
For instance, we write:
(async () => {
const d = await Promise.resolve(1)
console.log(d);
})();
to call Promise.resolve
with 1 to return a promise that resolves to 1.
Then we use the await
keyword to get the resolved value and assign that to d
.
Therefore, d
is 1.
Conclusion
To make a promise return the resolved value instead of [object Promise] with JavaScript, we can use the await
keyword.