Skip to content

Commit

Permalink
Convert nonce background calls to async/await (#12853)
Browse files Browse the repository at this point in the history
The calls to the background in `actions.js` that relate to the custom
nonce feature now use `async/await` and `promisifiedBackground`.

The behaviour should be unchanged except that when setting the nonce
field, the warning is shown in case of error before the loading
indicator drops, which seems like it would be a minor improvement (if
it has any user-facing impact at all).
  • Loading branch information
Gudahtt authored Dec 1, 2021
1 parent 9babc8b commit b6b202c
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions ui/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2135,15 +2135,15 @@ export function setUseBlockie(val) {
}

export function setUseNonceField(val) {
return (dispatch) => {
return async (dispatch) => {
dispatch(showLoadingIndication());
log.debug(`background.setUseNonceField`);
background.setUseNonceField(val, (err) => {
dispatch(hideLoadingIndication());
if (err) {
dispatch(displayWarning(err.message));
}
});
try {
await background.setUseNonceField(val);
} catch (error) {
dispatch(displayWarning(error.message));
}
dispatch(hideLoadingIndication());
dispatch({
type: actionConstants.SET_USE_NONCEFIELD,
value: val,
Expand Down Expand Up @@ -2857,19 +2857,17 @@ export function setNextNonce(nextNonce) {
}

export function getNextNonce() {
return (dispatch, getState) => {
return async (dispatch, getState) => {
const address = getState().metamask.selectedAddress;
return new Promise((resolve, reject) => {
background.getNextNonce(address, (err, nextNonce) => {
if (err) {
dispatch(displayWarning(err.message));
reject(err);
return;
}
dispatch(setNextNonce(nextNonce));
resolve(nextNonce);
});
});
let nextNonce;
try {
nextNonce = await promisifiedBackground.getNextNonce(address);
} catch (error) {
dispatch(displayWarning(error.message));
throw error;
}
dispatch(setNextNonce(nextNonce));
return nextNonce;
};
}

Expand Down

0 comments on commit b6b202c

Please sign in to comment.