0

Question:

I'm writing a promise retry function (that will retry promise on 'x' attempts).

Problem:

However, I am not able to understand why .catch() block is not getting invoked. It complains that there's no catch block (even though I have one)

How do I make sure the catch block is invoked?

This is my code:

// This function waits for 'timeout'ms before resolving itself.
function wait(timeout) {
    return new Promise((resolve, reject) => {
        setTimeout(function() {
            resolve();
        }, timeout)
    });
}

// Library function to retry promise.
function retryPromise(limit, fn, timeout=1000) {
    return new Promise((resolve, reject) => {
        fn().then(function(val) {
            return resolve(val);
        }, function(err) {
            if(limit === 0) return reject('boom finally');
            console.log("Retrying Promise for attempt", limit);
            wait(timeout).then(() => retryPromise(limit-1, fn, timeout));
        })
    });
}


// Caller
const print = retryPromise(3, demo);
print.then(function(result) {
    console.log(result);
}).catch(function(err){
    console.log('Inside catch block');
    console.error("Something went wrong", err);
});

function demo(val) {
    return new Promise((resolve, reject) => {
        setTimeout(function() {
            return reject(val || 1000);
        }, 1000);
    });
}

What am I missing in the code? Please elucidate.

TechnoCorner
  • 4,879
  • 10
  • 43
  • 81
  • 1
    Your `retryPromise` function is broken, it doesn't forward errors (or, any results) from the recursive call. – Bergi Aug 25 '19 at 19:17
  • Not sure why it's flagged as duplicate. @Bergi the article tagged does not really explain why my code is failing. Any help would be much appreciated. – TechnoCorner Aug 25 '19 at 19:23
  • Yes, the `retryPromise` function is "*retrying promise for attempt*", but in that case it never resolves (neither fulfill nor reject) the `print` promise that it had returned. Use promise chaining, do not use the `new Promise` constructor in the `retryPromise` implementation. – Bergi Aug 25 '19 at 19:24
  • Yes that's expected because I want to reject all 3 attempts and I want to go see the promise go to `catch()` block. This is intentional. But the issue here is that, the catch block is not properly getting invoked. (or am i missing something?) – TechnoCorner Aug 25 '19 at 19:26
  • Let's disregard the `catch` block at first. Your `retryPromise` function doesn't even work in case one of the retries successfully produces a result. Surely you want to fulfill `print` if `demo` works on the second attempt? Try it out. – Bergi Aug 25 '19 at 19:28
  • It's just like a recursive function missing a `return` statement for the recursive call. – Bergi Aug 25 '19 at 19:29
  • Use a `demo` function that `reject`s on the first call but `resolve`s on the second call. (Have an external counter for that). Not sure whether you can see my edit to your repl.it. – Bergi Aug 25 '19 at 19:41
  • Can you share your Repl? I don't think it allows to edit the original repl (it makes a copy) thanks again! I'm just learning this. @Bergi – TechnoCorner Aug 25 '19 at 20:14
  • https://repl.it/repls/SecondAcceptableCalculator – Bergi Aug 25 '19 at 20:26
  • Thanks for the code. Trying to figure out what's going wrong. I'm not even getting any responses since it's tagged as duplicate. Can you pls help? – TechnoCorner Aug 26 '19 at 15:54
  • Did my repl.it not help you to figure out what's going wrong? You never call `resolve` or `reject` in the recursive case. (And the duplicate shows that you it you had used promise chaining instead of the `new Promise` constructor, you would've never ran into the problem) – Bergi Aug 26 '19 at 16:03

0 Answers0