I have an API that returns JSON, where the value that I need is stored in either one of the two object properties:
data.d.GetContextWebInformation.FormDigestValue
or
data.value.GetContextWebInformation.FormDigestValue
The code that I'm using is pretty clumsy. At first, I used something like this:
if (data && data.d && data.d.GetContextWebInformation && data.d.GetContextWebInformation.FormDigestValue) {
this.options.token = data.d.GetContextWebInformation.FormDigestValue;
} else {
// check for second one
Now I've rewritten it using try/catch, and it looks a bit cleaner, but still clumsy:
var token;
try {
// If Verbosity was odata=verbose:
token = data.d.GetContextWebInformation.FormDigestValue;
} catch (ex) {
try {
// If Verbosity was odata=nometadata or odata=minimalmetadata:
token = data.value.GetContextWebInformation.FormDigestValue;
} catch (ex) {
throw 'Unable to obtain authorization token.';
}
} finally {
if (token) { this.options.token = token; }
}
What would be a better way to write the same code for a cleaner, nicer look? If that matters, I need to support IE9 so probably can't use newer language features.