如何延迟运行此函数 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)
一样使用它,而不是不适合您的代码。