首页 > 解决方案 > 可迭代和异步可迭代对象的流注释?

问题描述

我有一个函数foo可以返回一个可以迭代的对象:

const foo = () => ({
  [Symbol.iterator]: function * () {
    yield * [ 1, 2, 3 ]; 
  }, 
});

for (const x of foo()) {
  console.log(x);
}

// 1
// 2
// 3

现在我想foo用 Flow 类型定义进行注释。但是这些看起来像什么?

另外,async版本bar呢?

const bar = () => ({
  [Symbol.asyncIterator]: async function * () {
    yield * [ 1, 2, 3 ]; 
  }, 
});

for await (const x of bar()) {
  console.log(x);
}

标签: javascriptasync-awaitgeneratorflowtype

解决方案


推荐阅读