Created
May 28, 2014 19:30
-
-
Save davidbiehl/92920ad5d9be9892f8bc to your computer and use it in GitHub Desktop.
Waiting for multiple AJAX requests, the latter is conditional
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new RSVP.promise(function(resolve, reject) { | |
var promise = new RSVP.promise(function(resovle, reject) { | |
request({ | |
dataType: 'json', | |
url: window.ENV.prependHost + "/sa/api/v2/auth" | |
}) | |
.then(resolve) // if the first is a success, just resolve! | |
.catch(function() { // not a success, trying again | |
request({ | |
dataType: 'json', | |
url: window.ENV.prependHost + "/sa/api/v2/auth" | |
}) | |
.then(resolve) // the second is a success, resolve! | |
.catch(reject) // the second failed :( reject | |
}); | |
}); | |
var promise2 = new RSVP.promise(function(resolve, reject) { | |
promise.then(function() { | |
request({ | |
dataType: 'json', | |
url: 'the/url/for/user/data' | |
}).then(resolve).catch(reject); | |
}) | |
}); | |
promise2.then(function() { | |
// should resolve the user data upon successful auth | |
}).catch() { | |
// either we couldn't auth | |
// or the user data wasn't retrieved. | |
// in a simple case, we don't know what ... | |
// but you could reject with an arg to indicate which | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment