M
M
Meros2019-08-09 16:58:37
JavaScript
Meros, 2019-08-09 16:58:37

How to prevent the function from advancing further?

Good evening. I'm new to js, ​​I won't post the full component as it's almost 500 lines long and it will be a pain to understand it.

async created(){
  await this.getUserCheckList();
  ....последующие функции и обработка данных.
}
methods:{
  async getUsersCheckList(){
    await axios.get(this.userListUrl).then((x)=>{this.usernameSlugList = x.data.slug; this.emailSlugList = 
    x.data.email_slug}).catch((e)=>{this.handleUsersListErrors(e)});
  },
  async handleUsersListErrors(e){
    if (!e.status){
      await setTimeout(()=> this.getUsersCheckList(), 10000);
     }
  },
}

The idea is as follows: if it was not possible to connect to the server, then the requested function is called through async handleUsersListErrors until the server starts to respond. And until that happens, async created() should not move beyond await this.getUserCheckList() . What happens to me is that after the first call to handleUserListErrors , the processes in created move on, and this causes a ton of errors that I would like to avoid. Actually, the question is: how can I organize the code so that in async created() does not move further until this.getUserCheckList receives data?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-08-09
@Meros

await setTimeout

A pointless operation, setTimeout does not return a Promise, there is nothing to evate here.
If you want to use await along with setTimeout, you must explicitly wrap the latter in a Promise: In addition, you must return the results of executing functions, for example here
after the catch has finished, the call to getUserCheckList completes, and created continues to work, there will be no "don't let go".
UPD. Give an example of what an implementation of such a scheme might look like (the "request" is the generation of a random number, a successful result is a number that falls into the top 10% of the specified interval).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question