Deno top level await

for await...of statement is used to iterate through async iterators, and serve returns an async iterator, where each iteration will be a new incoming request.

Is it specific to deno or is it a top-level-await found in this tc39 proposal?

No, it's not specific to Deno and it's a different proposal than top-level await.


Here's a simple example of an asyncIterator that works in browsers too (non-deno exclusive)

const obj = {
   async *[Symbol.asyncIterator]() {
      for(let i = 0; i < 10; i++)
        yield new Promise(resolve => setTimeout(() => resolve(i), 100));
   }
};

(async() => {
  // You don't need the wrapper if the environment 
  // supports top-level await, e.g: Deno
  for await(const i of obj)
    console.log(`${i}`, new Date())

})();


Edit: Why can it be used outside of an async function?

Because Deno supports top-level await