There may be situations in which you have to use multiple nextTick calls in a single function. In this case, using ES8's async/await functionality is very useful, as it can prevent overly nested, unreadable code.
Right now, I have to use either this:
async myMethod() {
// code before first nextTick call
await new Promise(resolve => this.$nextTick(resolve));
// code before second nextTick call
await new Promise(resolve => this.$nextTick(resolve)); // ugly :(
// code after
}
Or this:
myMethod() {
// code before first nextTick call
this.$nextTick(() => {
// code before second nextTick call
this.$nextTick(() => { // ugly nesting :(
// code after
});
});
}
nextTick should return a Promise. It may also take a closure as a parameter for compatibility reasons.
You could then use all three variants:
this.$nextTick(() => /* my code after */);
this.$nextTick().then(() => /* my code after */);
await this.$nextTick();
/* my code after*/
It already returns Promise when you does not specify a callback.
https://vuejs.org/v2/api/#vm-nextTick
Most helpful comment
It already returns Promise when you does not specify a callback.
https://vuejs.org/v2/api/#vm-nextTick