Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"await" seems successfully extract all nested Promises, why?

I was interested in "await" behavior, so I ran this code

console.log(await Promise.resolve(Promise.resolve(Promise.resolve("hahaha"))));

in my browser console, the result is correct "hahaha". It seems there is a hidden loop in "await" till no Promise remains. I didn’t find explanation from MDN. Anybody knows why? Thanks.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

From the MDN documentation:

This function flattens nested layers of promise-like objects (e.g. a promise that fulfills to a promise that fulfills to something) into a single layer — a promise that fulfills to a non-thenable value.

"hahaha" is the non-thenable value that it fullfills to.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading