From b6b202ca13958dce0037410dff994083c7a5cc45 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Wed, 1 Dec 2021 15:16:10 -0330 Subject: [PATCH] Convert nonce background calls to async/await (#12853) 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). --- ui/store/actions.js | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/ui/store/actions.js b/ui/store/actions.js index 4c60e86804..c6e08e1a85 100644 --- a/ui/store/actions.js +++ b/ui/store/actions.js @@ -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, @@ -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; }; }