3

I am able to run below code on node js as client. It works but when I use the same code in react-native it throws below error.

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)]

I tried adding below modules but none of them work

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);
    })
  }
})();

actual output is an array of data.

enter image description here

Derek Pollard
  • 6,953
  • 6
  • 39
  • 59

1 Answers1

2

I was able to get around it with below solution. Instead of relying on Babel i used solution provided in here (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);
}
Edgar
  • 6,022
  • 8
  • 33
  • 66