(ES6) ES6 (2016)

WellKnownSymbols (WN01)

   1:  // Asynchronous generator and iterator
   2:  async function* asyncNumberGenerator() {
   3:      let i = 0;
   4:      while (i < 5) {
   5:          await new Promise(res => setTimeout(res, 1000));
   6:          yield i++;
   7:      }
   8:  }
   9:  const asyncIterator = asyncNumberGenerator()[Symbol.asyncIterator](); //Symbol.asyncIterator must be used to iterate over result of async generator function
  10:  
  11:  (async ()=>{  // async IIFE
  12:  
  13:      console.log(await asyncIterator.next())  // { value: 0, done: false }
  14:  
  15:  
  16:      console.log(await asyncIterator.next())  // (after approximately 1 second) { value: 1, done: false }
  17:  })();
  18:  
  19:  // Also can be used with for await...of loop:
  20:  // (async () => {
  21:  //     for await (const num of asyncNumberGenerator()) {
  22:  //         console.log(num) // Log number when promise resolved in async generator
  23:  //     }
  24:  // })();



Symbol.asyncIterator is used to obtain an asynchronous iterator from an asynchronous iterable, such as the one returned by an asynchronous generator function (async function*).

{ value: 0, done: false }
{ value: 1, done: false }





WellKnownSymbols context:






ES6 context:



Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23>  <24>  <25
Link to this page: http://www.vb-net.com/JavascriptES6/WN01.htm
<TAGS>  <ARTICLES>  <FRONT>  <CORE>  <MVC>  <ASP>  <NET>  <DATA>  <TASK>  <XML>  <KIOSK>  <NOTES>  <SQL>  <LINUX>  <MONO>  <FREEWARE>  <DOCS> <TRAVELS> <FLOWERS> <RESUME> < THANKS ME>