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
.