Skip to content

Latest commit

 

History

History
30 lines (26 loc) · 543 Bytes

2018-03-05__es2018.md

File metadata and controls

30 lines (26 loc) · 543 Bytes

for-await-of

异步迭代器

var bt = {
  [Symbol.asyncIterator]: () => {
    const items = 'for-await-of'.split('')
    return {
      next: () => {
        return new Promise(resolve => {
          setTimeout(() => {
            resolve({
              done: items.length === 0,
              value: items.shift()
            })
          }, 1000)
        })
      }
    }
  }
};

(async function(){
  for await (const item of bt) {
    console.log(item)
  }
})()