首页 > 解决方案 > React Native:调用 JSTimers.CallTimers 时出错

问题描述

我正在尝试在 Android(一加 6t)上运行我的应用程序。这在调用 firebase 之前运行良好,但是一旦我将该行添加onSend={Fire.shared.send}到 Chat.js,应用程序就会崩溃。日志只显示未捕获的错误:调用 JSTimers.CallTimers 时出错。在其他任何地方都没有看到这个错误。有谁知道是什么问题?

这是小吃:https ://snack.expo.io/@adititipnis/community

标签: androidfirebasereact-nativereact-native-androidexpo

解决方案


如果在将 JS 对象发送到本机端时省略了 await 调用,则可能会出现此错误,因此传递的是 promise 而不是结果。

我正在使用包装 setTimeout 的典型异步睡眠模式,因此这也可能是此错误呈现方式的一个因素,我不完全确定。

这是未经测试的,但这样的事情应该重现它:

// some async func
const asyncGetResult = async () => {
    await sleep(17);
    // etc.
    return Promise.resolve(result);
};    

// this should cause the error:
MyNativeComponent.nativeMethod({
    result: asyncFunc() // <- missing 'await'
});

// this should not cause the error:
MyNativeComponent.nativeMethod({
    result: await asyncFunc()
});

如果您不知道自己在寻找什么,这可能很难追踪。我采用了消除过程,逐个文件还原更改,直到找到有问题的行。希望这可以节省一些时间。


推荐阅读