javascript - ReactNative 异步生成器
问题描述
我正在尝试使用 ES6AsyncGenerator
函数从分页 API 检索我的 ReactNative 应用程序中的数据,但没有运气。我正在重用我已经能够在使用 ES6 和 React 的 Web 应用程序中成功使用的代码。我相当确定这将归结为某种配置问题或语言支持,但我无法弄清楚我的问题是什么,因为这些错误并不是特别有用。如果必须,我显然可以避免AsyncGenerator
并以另一种方式实施。
基本的生成器函数具有以下结构:
export async function* apiPager() {
let nextToken = null;
do {
const resp = await makeAPICall(nextToken);
nextToken = resp.nextToken;
yield resp;
} while(nextToken)
}
我用来处理生成器的代码如下:
const pager = apiPager(...)
console.log('pager', pager);
for await (const resp of pager) {
doThings(resp);
}
当我尝试使用此代码时,我在控制台中看到以下内容:
pager AsyncIterator {_invoke: ƒ}_invoke: ƒ enqueue(method, arg)arguments: (...)caller: (...)length: 2name: "enqueue"prototype: {constructor: ƒ}__proto__: ƒ ()[[FunctionLocation]]: runtime.js:167[[Scopes]]: Scopes[3]__proto__: next: ƒ (arg)return: ƒ (arg)throw: ƒ (arg)Symbol(Symbol.asyncIterator): ƒ ()constructor: ƒ AsyncIterator(generator)__proto__: Object
YellowBox.js:71 Possible Unhandled Promise Rejection (id: 0):
TypeError: _iterator[(intermediate value)(intermediate value)(intermediate value)] is not a function
TypeError: _iterator[(intermediate value)(intermediate value)(intermediate value)] is not a function
at WorkoutHistory.fetchSets$ (blob:http://localhost:8081/8ee39d52-49af-4c3e-a8a7-b3d585ce1223:163566:234)
at tryCatch (blob:http://localhost:8081/8ee39d52-49af-4c3e-a8a7-b3d585ce1223:23518:19)
at Generator.invoke [as _invoke] (blob:http://localhost:8081/8ee39d52-49af-4c3e-a8a7-b3d585ce1223:23693:24)
at Generator.prototype.<computed> [as next] (blob:http://localhost:8081/8ee39d52-49af-4c3e-a8a7-b3d585ce1223:23561:23)
at tryCatch (blob:http://localhost:8081/8ee39d52-49af-4c3e-a8a7-b3d585ce1223:23518:19)
at invoke (blob:http://localhost:8081/8ee39d52-49af-4c3e-a8a7-b3d585ce1223:23594:22)
at blob:http://localhost:8081/8ee39d52-49af-4c3e-a8a7-b3d585ce1223:23624:13
at tryCallTwo (blob:http://localhost:8081/8ee39d52-49af-4c3e-a8a7-b3d585ce1223:24829:7)
at doResolve (blob:http://localhost:8081/8ee39d52-49af-4c3e-a8a7-b3d585ce1223:24993:15)
at new Promise (blob:http://localhost:8081/8ee39d52-49af-4c3e-a8a7-b3d585ce1223:24852:5)
[![error messages][1]][1]
第一个问题似乎很明显,即 is 的类型,pager
而AsyncIterator
不是AsyncGenerator
我在 Web 应用程序中使用相同代码看到的类型。但据我了解,任何一个都应该可以通过for await (...)
循环操作,不是吗?一些奇怪的事情正在发生......如果可以的话请告诉我。
解决方案
嗯...我从来没有得到答案。我还发布了这个问题:https ://github.com/facebook/react-native/issues/27432并没有得到任何关注。
我确实找到了一个解决方法,我将在这里重新发布,这只是有点不令人满意:
解决方法(来自 github 问题)
async function* asyncGen() {
yield 'whatever';
}
const gen = asyncGen();
while (!gen.done)
console.log('result', (await gen.next()).value);
我想这只是有点烦人,因为与for await (... of ...)
编写生成器函数而不是创建迭代器的能力相比,语法改进的价值要小一些。但是,很高兴了解这里的问题所在。
推荐阅读
- android - 如何从开始硬编码 TextView 对齐距离?
- python - Python Ctypes,C++ 字符串到浮点数的转换在 python 中调用 Matplotlib 后给出错误/舍入的结果
- vim - 如何在 Vim 中重新映射命令 ":u" (UNDO)?
- linux - Wayland 应用程序中的“身份验证失败”
- python - 优化 groupby agg 函数以返回多个结果列
- r - GGplot2:scale_y_sqrt() 以错误的方式改变比例
- haskell - Hackage 是否会为候选包生成黑线鳕?
- hive - Sqoop 导出错误:目标数据库尚不支持混合更新/插入
- validation - 如何验证nestjs中的授权标头?
- python - 使用可选子查询将 sql 转换为具有别名的 sqlalchemy