How to use async/await in Vue.js?

You have to use either then or await not both as shown below: If using then created () { this.getA().then((result) => { console.log(1) console.log(2) this.getB() }) }, methods : { getA () { return $axios.post(`/getA`,params); }, getB (){ console.log(3) } } If using await async created (){ await this.getA() console.log(1) console.log(2) this.getB() }, methods : … Read more

Why doesn’t the code after await run right away? Isn’t it supposed to be non-blocking?

Just as its name implies, the await keyword will cause the function to “wait” until the corresponding promise resolves before executing the next line. The whole point of await is to provide a way to wait for an asynchronous operation to complete before continuing. The difference between this and blocking code is that the world … Read more

Why are await and async valid variable names?

Reserved keywords cannot be used as identifiers (variable names). Unlike most other special Javascript words (like those listed in the question, let, finally, …), await is not a reserved keyword, so using it as a variable name does not throw a SyntaxError. Why wasn’t it made into a reserved keyword when the new syntax came … Read more

Waiting for more than one concurrent await operation

TL;DR Don’t use the pattern in the question where you get the promises, and then separately wait on them; instead, use Promise.all (at least for now): const [value1, value2] = await Promise.all([getValue1Async(), getValue2Async()]); While your solution does run the two operations in parallel, it doesn’t handle rejection properly if both promises reject. Details: Your solution … Read more