如何让这个函数延迟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);
最佳答案
此 promise 将在 ms
毫秒内解析:
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
您可以像 await sleep(5000)
一样使用它,而不是使用不适合您的代码。
关于javascript - 如何在 react native 组件中延迟运行异步等待?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55348237/