gpt4 book ai didi

javascript - 在 native react 中使用 "for await...of"

转载 作者:行者123 更新时间:2023-11-30 19:34:00 25 4
gpt4 key购买 nike

我能够作为客户端在节点 js 上运行以下代码。它可以工作,但是当我在 react-native 中使用相同的代码时,它会抛出以下错误。

Require cycles are allowed, but can result in uninitialized values. Consider refactoring to remove the need for a cycle. 2019-05-14 13:47:57.271 [error][tid:com.facebook.react.JavaScript] 'Unhandled promise rejection', { [TypeError: _iterator[typeof Symbol === "function" ? typeof Symbol === "function" ? Symbol.iterator : "@@iterator" : "@@iterator"] is not a function. (In '_iteratortypeof Symbol === "function" ? typeof Symbol === "function" ? Symbol.iterator : "@@iterator" : "@@iterator"', '_iterator[typeof Symbol === "function" ? typeof Symbol === "function" ? Symbol.iterator : "@@iterator" : "@@iterator"]' is undefined)]

我尝试添加以下模块,但没有一个起作用

npm install --save @babel/polyfill es6 polyfill

import asyngularClient from 'asyngular-client';

let socket = asyngularClient.create({
port: 8000
});

(async () => {
let channel = socket.subscribe(inboxChannel, { customData: 'yiyiyi?' });

for await (let data of channel) {
// ... Handle channel data.
console.log('channel: ', inboxChannel, data);
const messages = [];
data.forEach(d => {
messages.unshift(d);
});
messages.forEach(m => {
console.log(m.content);
})
}
})();

实际输出的是一个数据数组。

enter image description here

最佳答案

我能够通过以下解决方案绕过它。我没有依赖 Babel,而是使用了此处提供的解决方案 (Using asynchronous iteration via Babel)。

let channel = await socket.subscribe(inboxChannel, { customData: 'yiyiyi?' });
const iterator = channel[Symbol.asyncIterator]();
const result = [];
while (result.length < Infinity) {
const { value, done } = await iterator.next();
if (done) break;
result.push(value);
console.log(value);
}

关于javascript - 在 native react 中使用 "for await...of",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56136013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com