So the whole Top-level await
thing is completely flying over my head. I don’t get it. How is top level wait any different than a top level promise? I am not being argumentative, I honestly think I am missing something here. I’ve been writing a parser that parses some JSON stuff which pertains to a UI (details about the parser are irrelevant so I will spare you from having to read them). I created a simple to read, comprehend & understand interface that is asynchronous. I wrote it in TypeScript (nightly v4.7.0) Node v17.6.0 & configured the project as an ESM module so that I knew I would have support for Top Level Awaits.
Using a Top Level await
I executed the following asynchronous api method call.
await jsonFile
.readData('./src/res/test-theme.json')
.then((data) => {
console.log('Printing JSON-Files Content:n%s', data.toString());
});
What I don’t understand, is why would you care about top level await, when you can just do the same thing w/o await, for example:
jsonFile
.readData('./src/res/test-theme.json')
.then((data) => {
console.log('Printing JSON-Files Content:n%s', data.toString());
});
I know that I use promises all the time, and I pretty much never reach for async, or await, consequently; I am likely thinking with a one track promise based mind. To put this post in clear question format, where is the benefit in using a top level await over a top level promise?