Vue: `nextTick` should return a Promise to support ES8's async/await

Created on 21 Jan 2018  路  1Comment  路  Source: vuejs/vue

What problem does this feature solve?

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
        });
    });
}

What does the proposed API look like?

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*/

Most helpful comment

It already returns Promise when you does not specify a callback.
https://vuejs.org/v2/api/#vm-nextTick

>All comments

It already returns Promise when you does not specify a callback.
https://vuejs.org/v2/api/#vm-nextTick

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bfis picture bfis  路  3Comments

loki0609 picture loki0609  路  3Comments

wufeng87 picture wufeng87  路  3Comments

gkiely picture gkiely  路  3Comments

aviggngyv picture aviggngyv  路  3Comments