rpominov/fun-task

task.retry([maxRetries])

rpominov opened this issue · 1 comments

Extracted from #17 (comment)

simple demo of an implementation with two callbacks to control the retry strategy

// helper
delayTaskComputation = function(dT)
{
	return function computation(succeed)
	{
		const timer = setTimeout(succeed,dT);

		return function () { clearTimeout(timer); };
	};
}
/**
* With custom retry strategy
* @param {function(Error): Boolean} isRetryable -  return false to abort
* @param {function(Error): Number} delay
* @param {FunTask.Task} task
* @return {FunTask.Task}
*/
var retry = function(isRetryable, delay, task){
	return task.orElse(function failure(error)
	{
		if (isRetryable(error))
		{
			return Task.create(delayTaskComputation(delay(error)))
				.chain(function(){ return retry(isRetryable, delay, task); });
		}
		return Task.rejected(error);
	});
};