Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I run async await with delay in react-native component?

How can run this function with delay for 5 second?

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');
  };

I tried these:

 await Task.Delay(3);

And

 await timeout(5000);

And

 await sleep(2000);
like image 243
Areza Avatar asked Oct 22 '25 15:10

Areza


1 Answers

This promise resolves in ms milliseconds:

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))

You use it like await sleep(5000) instead of the code that didn't work for you.

like image 156
Nino Filiu Avatar answered Oct 24 '25 04:10

Nino Filiu