如何在反应本机组件中延迟运行异步等待



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

最新更新