Considering that I have code that uses callbacks and I'm trying to wrap it in promises, is the following code a mistake or anti-pattern?
function getDateAsync(onSuccess, onFailure) {
...
}
function getValidDate() {
return new Promise((resolve, reject) => {
getDateAsync((date) => {
checkValidDate(date, resolve, reject);
}, () => {
markDateInvalid();
reject();
}
});
}
function checkValidDate(date, resolve, reject) {
if (isValid(date)) {
resolve(date);
} else {
markDateInvalid();
reject();
}
}
function markDateInvalid() { ... }
This is assuming that checkValidDate is more complicated, and can't be inlined, and that markDateInvalid needs to be called in both of the instances indicated.
Is there a better way to write this code?
Update: For the sake of this example, we should also assume that getDateAsync is an external library, or is otherwise expensive and infeasible to convert to using Promises.