admin管理员组

文章数量:1289868

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

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);
Share Improve this question edited Mar 26, 2019 at 0:32 Areza asked Mar 26, 2019 at 0:28 ArezaAreza 7212 gold badges15 silver badges30 bronze badges 3
  • 1 setTimeout? . – zerkms Commented Mar 26, 2019 at 0:30
  • May be it's wrong. I googled it. – Areza Commented Mar 26, 2019 at 0:32
  • You put inside the function setTimeout(function() { // run here the user token }, 5000); . So bootstrapAsync = async () => { // setTimeout } – ABC Commented Mar 26, 2019 at 0:34
Add a ment  | 

1 Answer 1

Reset to default 9

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.

本文标签: javascripthow can I run async await with delay in reactnative componentStack Overflow