Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debounce effect #50

Closed
mrcha90 opened this issue Jan 21, 2017 · 4 comments
Closed

Debounce effect #50

mrcha90 opened this issue Jan 21, 2017 · 4 comments

Comments

@mrcha90
Copy link

mrcha90 commented Jan 21, 2017

Is it possible to add debounce effect functionality? It is little bit annoying to see red error (for example "email") after first letter was written. Or when I have async call, I don't want to call api after every single keypress. Better solution would be wait 1,2 seconds and then call async function. Really appreciate simplicity of Vuelidate!

@mrcha90
Copy link
Author

mrcha90 commented Jan 22, 2017

I have found solution in your prev issue. So it's ok.

@mrcha90 mrcha90 closed this as completed Jan 22, 2017
@Frizi
Copy link
Contributor

Frizi commented Jan 22, 2017

Could you please add a reference where have you found it? Creating list of useful examples is on a roadmap for the docs.

@shyamchandranmec
Copy link

@mrcha90
Could you please add your solution here.

@RoundRobin-fr
Copy link

_.debounce from lodash does not return the result of the debounced function so the validator cannot work.

You could use this debounce version to make sure every call to the debounced function resolves their promise with the result of the latest call during the interval.

const debounce = (callback, timeout) => {
    let timer;
    const resolvers = [];
    return async (...args) => {
        clearTimeout(timer);
        return new Promise((resolve) => {
            resolvers.push(resolve);
            timer = setTimeout(async () => {
                const result = await callback.apply(this, args);
                resolvers.map((resolver) => resolver(result));
            }, timeout);
        });
    };
};

const rules = {
    name: {
        test: helpers.withAsync(
            debounce(() => {
                return false;
            }, 1000)
        ),
        $lazy: true,
    },
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants