javascript - 如何在 react-native 组件中运行 async await 并延迟?
问题描述
如何延迟 5 秒运行此功能?
export default class Splash extends React.PureComponent {
constructor() {
super();
this._bootstrapAsync();
}
bootstrapAsync = async () => {
//await Task.Delay(5);
//await timeout(5000)
//await sleep(5000);
const userToken = await AsyncStorage.getItem('userToken');
this.props.navigation.navigate(userToken ? 'App' : 'Auth');
};
我试过这些:
await Task.Delay(3);
和
await timeout(5000);
和
await sleep(2000);
解决方案
这个承诺在ms
几毫秒内解决:
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
您可以像使用它await sleep(5000)
而不是对您不起作用的代码一样使用它。
推荐阅读
- r - R markdown 无法在 tibble 中显示汉字
- c# - 从字符串 C# 中删除 \
- flutter - 选择颤振构建 apk 版本?(abi)
- azure-cosmosdb - Cosmos DB 定价
- reactjs - 为 react datepicker 添加 CSS 样式并改进 datepicker 输入边界
- flutter - Flutter Navigator 2.0 在页面之间传递数据
- google-apps-script - Google 表格脚本 TOC 目录 超链接的选项卡列表
- php - 获取 WooCommerce 社交登录以在登录后重定向到以前的 URL
- java - java.lang.AssertionError:预期:-1.0 实际:<12.0>
- google-app-engine - 将 Google Compute Engine 上的应用移至 Google App Engine