Using lodash debounce to return a promise Using lodash debounce to return a promise reactjs reactjs

Using lodash debounce to return a promise


This sandbox fixes your problem.

You should not create a new debounce function on every render with:

return new Promise(resolve => {  debounce(() => resolve(this.getIsNameUnique(name)), 2000);});

Instead you should just wrap your whole function isNameUnique with the debounce (see my sandbox). By creating a new debounce function on every hit, it cannot 'remember' that is was called or that is will be called again. This will prevent the debouncing.

Additionally, by making async getIsNameUnique you can reduce the complexity of it be just using await.