用于遍历异步操作的集合

  1. const list = [ promise1, promise2, promise3 ]
  2. async function test () {
  3. for await ( const item of list ) {
  4. console.log(Date.now(), item)
  5. }
  6. }

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for-await…of
https://es6.ruanyifeng.com/#docs/async-iterator

部署接口

  1. const obj = {
  2. i: 0,
  3. [Symbol.asyncIterator] = function () {
  4. return {
  5. next: () => {
  6. if (this.i < 3) {
  7. return Promise.resolve({ value: this.i++, done: false })
  8. }
  9. return Promise.resolve({ done: true })
  10. }
  11. }
  12. }
  13. }
  14. obj = function () {
  15. return {
  16. i: 0
  17. next () {
  18. return Promise.resolve({ value: this.i++, done: false })
  19. }
  20. }
  21. }