Friday 15 May 2015

javascript - Promises: Repeat operation until it succeeds? -



javascript - Promises: Repeat operation until it succeeds? -

i want perform operation repeatedly, increasing timeout between each operation, until succeeds or amount of time elapses. how construction promises in q?

all answers here complicated in opinion. kos has right thought can shorten code writing more idiomatic promise code:

function retry(operation, delay) { homecoming operation().catch(function(reason) { homecoming q.delay(delay).then(retry.bind(null, operation, delay * 2)); }); }

and comments:

function retry(operation, delay) { homecoming operation(). // run operation catch(function(reason) { // if fails homecoming q.delay(delay). // delay // retry more time then(retry.bind(null, operation, delay * 2)); }); }

if want time out after time (let's 10 seconds , can do:

var promise = retry(operation, 1000).timeout(10000);

that functionality built right q, no need reinvent :)

javascript promise q

No comments:

Post a Comment