Skip to content

Commit

Permalink
refactor: change .then and .catch to await in try-catch (#9731)
Browse files Browse the repository at this point in the history
refs: Agoric/documentation#1158

## Description

Refactor to clarify some code by using `try { await... } catch (e) {...}` rather than calling a promise's `.then` and `.catch` methods explicitly.

### Documentation Considerations

Updated `swapExample.contract.js` in Agoric/documentation#1158 as well.
  • Loading branch information
mergify[bot] authored Jul 20, 2024
2 parents 8d1be18 + ea82b9a commit c87930e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
16 changes: 8 additions & 8 deletions packages/orchestration/src/examples/swapExample.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { withOrchestration } from '../utils/start-helper.js';
* @param {Amount<'nat'>} offerArgs.staked
* @param {CosmosValidatorAddress} offerArgs.validator
*/
const stackAndSwapFn = async (orch, { localTransfer }, seat, offerArgs) => {
const stakeAndSwapFn = async (orch, { localTransfer }, seat, offerArgs) => {
const { give } = seat.getProposal();

const omni = await orch.getChain('omniflixhub');
Expand All @@ -52,12 +52,12 @@ const stackAndSwapFn = async (orch, { localTransfer }, seat, offerArgs) => {
slippage: 0.03,
});

await localAccount
.transferSteps(give.Stable, transferMsg)
.then(_txResult =>
omniAccount.delegate(offerArgs.validator, offerArgs.staked),
)
.catch(e => console.error(e));
try {
await localAccount.transferSteps(give.Stable, transferMsg);
await omniAccount.delegate(offerArgs.validator, offerArgs.staked);
} catch (e) {
console.error(e);
}
};

/** @type {ContractMeta<typeof start>} */
Expand Down Expand Up @@ -105,7 +105,7 @@ const contract = async (zcf, privateArgs, zone, { orchestrate, zoeTools }) => {
const swapAndStakeHandler = orchestrate(
'LSTTia',
{ zcf, localTransfer: zoeTools.localTransfer },
stackAndSwapFn,
stakeAndSwapFn,
);

const publicFacet = zone.exo('publicFacet', undefined, {
Expand Down
38 changes: 20 additions & 18 deletions packages/vow/src/when.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,26 @@ export const makeWhen = (
if (seenPayloads.has(vowV0)) {
throw Error('Vow resolution cycle detected');
}
result = await basicE(vowV0)
.shorten()
.then(
res => {
seenPayloads.add(vowV0);
priorRetryValue = undefined;
return res;
},
e => {
const nextValue = isRetryableReason(e, priorRetryValue);
if (nextValue) {
// Shorten the same specimen to try again.
priorRetryValue = nextValue;
return result;
}
throw e;
},
);

try {
// Shorten the vow to the "next step", whether another vow or a final
// result.
const res = await basicE(vowV0).shorten();

// Prevent cycles in the resolution graph.
seenPayloads.add(vowV0);
priorRetryValue = undefined;
result = res;
} catch (e) {
const nextRetryValue = isRetryableReason(e, priorRetryValue);
if (!nextRetryValue) {
// Not a retry, so just reject with the reason.
throw e;
}

// Shorten the same specimen to try again.
priorRetryValue = nextRetryValue;
}
// Advance to the next vow.
payload = getVowPayload(result);
}
Expand Down
2 changes: 2 additions & 0 deletions scripts/registry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ integrationTest() {
# Install the Agoric CLI on this machine's $PATH.
case $1 in
link-cli | link-cli/*)
# Prevent retries from failing with "must not already exist"
rm -f "$HOME/bin/agoric"
yarn link-cli "$HOME/bin/agoric"
persistVar AGORIC_CMD "[\"$HOME/bin/agoric\"]"
;;
Expand Down

0 comments on commit c87930e

Please sign in to comment.