0

I am checking the library promise-retry

and I can see that they do the following

promiseRetry(function (retry, number) {     
    return doSomething()
    .catch(retry);
})
.then(function (value) {
    // ..
}, function (err) {
    // ..
});

I am using async/await throughout the project, so I have converted it to

const resp = await promiseRetry((retry, number) => doSomething().catch(retry));

So this will work to get the value when the retrying works, but what about cases of failure? In the original code they have function(err), but how can this be replicated? It would have been possible if it was a .catch(err), but it's more of callback to the then function.

AngularDebutant
  • 1,436
  • 5
  • 19
  • 41
  • `.then(f, g)` is *the same* as `.then(f).catch(g)`. The second argument is the error handler. – VLAZ Mar 01 '21 at 12:44
  • Have you tried something like this: [Correct Try...Catch Syntax Using Async/Await](https://stackoverflow.com/q/44663864/4642212)? Or any [similar Google result](https://google.com/search?q=site%3Astackoverflow.com+js+catch+in+async+await)? – Sebastian Simon Mar 01 '21 at 12:44
  • 1
    @VLAZ - No, it isn't. There's a big difference between them. In the latter, any rejection triggered by `f` will go to `g`. In the former, it will not. (Also, the former creates one promise, the latter creates two.) – T.J. Crowder Mar 01 '21 at 12:44
  • What is the `promiseRetry` function? How does it handle decrementing `number`? – T.J. Crowder Mar 01 '21 at 12:47
  • @T.J.Crowder number is not so important here, but it just means the number of retries before the promiseRetry fails. The implementation of the function is here https://github.com/IndigoUnited/node-promise-retry/blob/7fb08491112cffe5a0dd11805eff20ca6b6133ac/index.js#L12 – AngularDebutant Mar 01 '21 at 12:50

2 Answers2

1

The answer depends on what the code in the ... of the original is. If the original is:

promiseRetry(function (retry, number) {     
    return doSomething()
    .catch(retry);
})
.then(function (value) {
    // ...using `value` here...
}, function (err) {
    // ...using `err` here...
});

then because that code is using the two-argument version of then, the near-equivalent using async/await is a bit of a pain:

let failed false;
let value;
try {
    value = await promiseRetry((retry, number) => doSomething().catch(retry));
} catch (err) {
    failed = true;
    // ...using `err` here...
}
if (!failed) {
    // ...using `value` here...
}

but with more context it might well be possible write something less cumbersome.

That said, most people would probably write this instead:

try {
    const value = await promiseRetry((retry, number) => doSomething().catch(retry));
    // ...using `value` here...
} catch (err) {
    // ...using `err` here...
}

The difference is whether the catch block catches errors during ...using `value` here.... It doesn't in the original but does in the "most people would probably write" above, which is closer to:

promiseRetry(function (retry, number) {     
    return doSomething()
    .catch(retry);
})
.then(function (value) {
    // ...using `value` here...
})
.catch(function (err) {
    // ...using `err` here...
});

Not having the code in the rejection handler handle rejections from the code in the fulfillment handler definitely makes sense sometimes, and perhaps particularly in a retry scenario: If the original action succeeded but the code handling the result of it fails, you probably don't want to retry the original action. That may well be why the original code used the two-argument then rather than then/catch.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks that makes a lot of sense. But I am wondering, why would the developer of the package do it that way and not use catch instead? – AngularDebutant Mar 01 '21 at 13:30
  • @AngularDebutant - Presumably because they didn't want the code in the rejection handler to handle rejections from the code in the fulfillment handler. That makes sense in some cases, and perhaps particularly in retry scenarios: If the original action succeeded but the code handling the result fails, you probably don't want to retry the original action. – T.J. Crowder Mar 01 '21 at 13:33
0

In Javascript if you use the "await" keyword and a error occured it will be turned into an classic exception.

Async/await in JS

If it’s an error, the exception is generated — same as if throw error were called at that very place.

Edit : So you need to use the try/catch mechanism

Arcord
  • 1,724
  • 1
  • 11
  • 16