'setTimeout block execution code block in NativeEventEmitter callback
I am trying to send a string to the native side (android) with a delay. But setTimeout is blocking the execution. Could you explain what is happening?
const eventEmitter = new NativeEventEmitter(Module);
const waitFor = (delay) { return new Promise((resolve) => setTimeout(resolve, delay)); }
const onFrameUpdate = async (e: string) => {
// Ignore onFrameUpdate stream
if (isPaused) {
console.log('Js on Pause');
return;
}
isPaused = true;
console.log('Js working');
if (e.hasOwnProperty('frameIndex')) {
await Module.setLabelText(`Hello`) ; // -> this is set text on native side in UI
await waitFor(1000); // remove this line and everything will be working fine, but I need a delay.
await Module.setLabelText(`hello2`); // this text will never
isPaused = false;
}
};
const onFrameUpdateSubscription = eventEmitter.addListener( 'onFrameUpdate', onFrameUpdate );
Solution 1:[1]
In React Native setTimeout() will never working in background. It will be executed immediately after react native ui will become to front again.
I implement background timer setTimeout on objC and java side manually and it works as expected now.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alexufo |
