The below code uses Promises/A+ (specifically rsvp.js) to generate a token. It does so by either taking an existing token or downloading and parsing a token from a web page. Is it possible to chain this to LoadURIPromise more cleanly? Usually one can chain together promises without causing as much indentation drift.
new RSVP.Promise(function (resolve) {
if (TokenData) {
resolve(TokenData);
} else {
LoadURIPromise(TokenURL).then(function (xmlDoc) {
TokenData = xmlDoc.evaluate(
'/root/theToken',
xmlDoc.documentElement,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue.textContent;
resolve(TokenData);
});
}
}).then(function (token) {
//Use Token here
}