Skip to content

Commit

Permalink
chore: move timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
npty committed Jul 26, 2024
1 parent 164a19a commit 512af7c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
11 changes: 11 additions & 0 deletions common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

function timeout(prom, time, exception) {
let timer;

// Racing the promise with a timer
// If the timer resolves first, the promise is rejected with the exception
return Promise.race([prom, new Promise((resolve, reject) => (timer = setTimeout(reject, time, exception)))]).finally(() =>
clearTimeout(timer),
);
}

/**
* Validate if the input string matches the time format YYYY-MM-DDTHH:mm:ss
*
Expand Down Expand Up @@ -300,4 +310,5 @@ module.exports = {
prompt,
findProjectRoot,
toBigNumberString,
timeout,
};
13 changes: 1 addition & 12 deletions evm/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const {
httpPost,
sleep,
findProjectRoot,
timeout,
} = require('../common');
const {
create3DeployContract,
Expand Down Expand Up @@ -947,16 +948,6 @@ function isValidChain(config, chainName) {
}
}

function timeout(prom, time, exception) {
let timer;

// Racing the promise with a timer
// If the timer resolves first, the promise is rejected with the exception
return Promise.race([prom, new Promise((resolve, reject) => (timer = setTimeout(reject, time, exception)))]).finally(() =>
clearTimeout(timer),
);
}

async function relayTransaction(options, chain, contract, method, params, nativeValue = 0, gasOptions = {}, expectedEvent = null) {
if (options.relayerAPI) {
const result = await httpPost(options.relayerAPI, {
Expand Down Expand Up @@ -1047,7 +1038,6 @@ module.exports = {
getEVMBatch,
getEVMAddresses,
getConfigByChainId,
sleep,
printWalletInfo,
wasEventEmitted,
isContract,
Expand All @@ -1063,7 +1053,6 @@ module.exports = {
getSaltFromKey,
getDeployOptions,
isValidChain,
timeout,
getAmplifierKeyAddresses,
getContractConfig,
relayTransaction,
Expand Down

0 comments on commit 512af7c

Please sign in to comment.