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();
}
9
  • 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. Commented Feb 28, 2020 at 0:48
  • If neither of the two things that the function does is asynchronous, you don't need - and should not use - async/await. Commented Feb 28, 2020 at 0: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 Commented Feb 28, 2020 at 1: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. Commented Feb 28, 2020 at 1:27
  • 1
    Await should go on the right-hand side. a = await b Commented Feb 28, 2020 at 2:17

2 Answers 2

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

Code Sandbox

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.