Skip to content

Instantly share code, notes, and snippets.

@msangel
Last active February 25, 2016 11:45
Show Gist options
  • Save msangel/35a7f6f678b9db2af651 to your computer and use it in GitHub Desktop.
Save msangel/35a7f6f678b9db2af651 to your computer and use it in GitHub Desktop.
this is function for creating tryable promise. as far as promise is not reusable, this accept promise factory, that create new promise for each try
var createTryablePromise = function (promiseFactory, tryes, delay) {
return new Promise(function (resolve, reject) {
var processOnce = function () {
promiseFactory().then(function (data) {
resolve(data);
}).catch(function (err) {
tryes--;
console.log('promise execution fail in:'+ tryes);
if(tryes>0){
if(typeof delay === 'undefined'){
console.log('its bigger then 0, so executing once again:');
processOnce();
} else {
setTimeout(function(){
console.log('its bigger then 0, so executing once again after some time:');
processOnce();
}, delay);
}
} else {
console.log('sorry, you have used all your tries');
reject(err);
}
});
};
processOnce();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment