0

I have a function that does 2 things. I want to wait for the first line and then execute the second line. The first line is a simple assignment. How can I do this using async-await without making the first assignment a function. The following code gives me an error in line 2.

async eg(){
    await a = b;
    c.focus();
}
mha
  • 551
  • 2
  • 11
  • 22
  • 1
    You don't have to wait for an assignment. Assignments are synchronous anyway so it'll always 'wait' for it finish before proceeding to the next line. – skirtle Feb 28 '20 at 00:48
  • If neither of the two things that the function does is asynchronous, you don't need - and should not use - `async`/`await`. – Bergi Feb 28 '20 at 00:56
  • I have a div with `v-if` that comes true only when a is equal to b. when that happens a pop up shows that contains c and then I want the focus to move to the c. Without the async-await it doesn't work – mha Feb 28 '20 at 01:13
  • 1
    Agreed with the two previous comments: you don't need `await` for an assignment. Can you post an extract of your template? It might be related to some "Component does not update when I want to" question. Stuff like `this.$nextTick();` can help. – Al-un Feb 28 '20 at 01:27
  • 1
    Await should go on the right-hand side. a = await b – Arc Feb 28 '20 at 02:17
  • @Al-un 's approach worked very well – mha Feb 28 '20 at 03:12
  • Thanks @mha. For the sake of future readers, can you add more details to your details? `$nextTick()` can address multiple issues so it is difficult to see to which problem it answers. Then this question might be a duplicate of or just derived from https://stackoverflow.com/a/55932110/4906586 – Al-un Feb 28 '20 at 08:36
  • Also, either please expand your answer and accept or I'll write an answer but this question should be marked as "closed" – Al-un Feb 28 '20 at 08:37
  • Accepted ChrisWong's answer as the complete one. Thanks :) – mha Feb 28 '20 at 17:47

2 Answers2

1
eg() {
  this.a = this.b;
  setTimeout(() => {
    this.$refs["c"].focus();
  }, 1);
}

Code Sandbox

ChrisWong
  • 20
  • 1
  • 3
0

As user Al-un mentioned using this.$nextTick(); solved it beautifully.

mha
  • 551
  • 2
  • 11
  • 22