Areza
Areza

Reputation: 721

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

Upvotes: 4

Views: 10537

Answers (1)

Nino Filiu
Nino Filiu

Reputation: 18503

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.

Upvotes: 9

Related Questions