I wrote some code for an application that uses the following pattern:
function _getData() {
return new Promise(function(resolve, reject) {
if(_hasDataTypeA()) {
SomeBackendAccessObject.getTypeAData().then(resolve, reject);
} else {
SomeBackendAccessObject.getTypeBData().then(_convertToTypeA).then(resolve, reject);
}
});
}
My rationale for writing the code this way is that if _hasDataTypeA
throws an exception, we're still able to return a Promise. This means the caller doesn't have to do a try
block and a Promise .catch
; but this code feels smelly to me. Is there a better way to write it?
_hasDataTypeA
can throw an exception, or did you mean that it can returnfalse
? \$\endgroup\$_hasDataTypeA
could, hypothetically, throw an exception. \$\endgroup\$_getData
can see it by calling.catch
, but I'm not entirely confident in this premise either. \$\endgroup\$