So, mosh teaches a deprecated way of async validation cause i’m getting an error saying isAsync is deprecated and that we should make it a promise which is really confusing to me. Since, i don’t want to learn deprecated information before continuing the course. I’m asking anyone here to show me how to refactor this part below into the new syntax. I looked at the documentation and tried to refactor it myself with no luck.
Deprecated Code
tags: {
type: Array,
validate: {
isAsync: true,
validator: function (v, callback) {
setTimeout(() => {
const result = v && v.length > 0;
callback(result);
}, 4000);
},
message: "A course should have at least one tag",
},
},
The above solutions were close but didn’t quite work for me. In this solution the validator creates a Promise, and uses an if…else to either resolve() or reject().
tags: {
type: Array,
validate: {
validator: function (v) {
return new Promise((resolve, reject) => {
setTimeout(() => {
// Do some async work
const result = v && v.length > 0;
if (result) {
resolve();
} else {
reject(new Error('A course should have at least one tag.'));
}
}, 4000);
});
},
},
},