Last active
February 25, 2016 11:45
-
-
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
This file contains hidden or 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
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