I've been trying to figure out how to best implement retrying e.g. a failed download using promises. My best guess is to resolve a promise with a new promise (see the pseudocode below), but all I could find in the documentation is that Promise.resolve() can be called with a thenable (and so, I believe, with a Promise). Does this apply to the resolve callback in the promise constructor as well? Is my approach correct or is there a better way to implement a retry?
function getdata(url) {
return new Promise(function(resolve, reject) {
ajaxcall({
url: url,
success: function(data) { resolve(data); },
failure: function(err) {
if(retriable(err)) {
resolve(getdata(url));
}
else {
reject(err);
}
}
});
});
}