I am working on redux-form atm and found the piece of code. Its working for me but is there any cleaner way to write this in ES6 style?
const asyncValidate = (values/* , dispatch */) => {
return new Promise((resolve, reject) => {
try {
if (['john', 'paul', 'george', 'ringo'].includes(values.name)) {
const error = {
name: 'That username is taken'
};
throw error;
}
resolve();
} catch (e) {
reject(e);
}
});
};
I would appreciate your help
Solution
const asyncValidate = (values/* , dispatch */) => {
return new Promise((resolve, reject) => {
const errors = {};
if (['john', 'paul', 'george', 'ringo'].includes(values.name)) {
errors.name = 'That username is taken';
}
reject(errors);
});
};
probably cleaner way?!