diff --git a/lib/actions/user-actions.js b/lib/actions/user-actions.js --- a/lib/actions/user-actions.js +++ b/lib/actions/user-actions.js @@ -96,13 +96,13 @@ import { authoritativeKeyserverID } from '../utils/authoritative-keyserver.js'; import { getConfig } from '../utils/config.js'; import { getMessageForException } from '../utils/errors.js'; +import { promiseWithTimeout } from '../utils/promises.js'; import { useDispatchActionPromise } from '../utils/redux-promise-utils.js'; import { useSelector } from '../utils/redux-utils.js'; import { usingCommServicesAccessToken, useIsRestoreFlowEnabled, } from '../utils/services-utils.js'; -import sleep from '../utils/sleep.js'; const loggedOutUserInfo: LoggedOutUserInfo = { anonymous: true, @@ -134,13 +134,11 @@ let response = null; try { - response = await Promise.race([ + response = await promiseWithTimeout( callKeyserverEndpoint('log_out', requests), - (async () => { - await sleep(500); - throw new Error('keyserver log_out took more than 500ms'); - })(), - ]); + 500, + 'keyserver log_out', + ); } catch {} const currentUserInfo = response ? loggedOutUserInfo : null; return { currentUserInfo, preRequestUserState, keyserverIDs }; @@ -223,13 +221,11 @@ }; } try { - await Promise.race([ + await promiseWithTimeout( callIdentityClientLogOut(), - (async () => { - await sleep(500); - throw new Error('identity log_out took more than 500ms'); - })(), - ]); + 500, + logOutType + ' identity log_out', + ); } catch {} })(); @@ -310,13 +306,11 @@ throw new Error('Identity service client is not initialized'); } try { - await Promise.race([ + await promiseWithTimeout( identityClient.logOut(), - (async () => { - await sleep(500); - throw new Error('identity log_out took more than 500ms'); - })(), - ]); + 500, + 'identity log_out', + ); } catch {} return { currentUserInfo: null, @@ -662,13 +656,11 @@ ? identityClient.deletePasswordUser(password) : identityClient.deleteWalletUser(); - await Promise.race([ + await promiseWithTimeout( deleteUserPromise, - (async () => { - await sleep(callIdentityServiceTimeout); - throw new Error('identity delete user call took more than 500ms'); - })(), - ]); + callIdentityServiceTimeout, + 'identity delete user call', + ); return { currentUserInfo: null, diff --git a/lib/utils/promises.js b/lib/utils/promises.js --- a/lib/utils/promises.js +++ b/lib/utils/promises.js @@ -1,5 +1,7 @@ // @flow +import sleep from './sleep.js'; + type Promisable = Promise | T; async function promiseAll }>( @@ -39,4 +41,23 @@ })(); } -export { promiseAll, promiseFilter, ignorePromiseRejections }; +function promiseWithTimeout( + promise: Promise, + timeout: number, + promiseDescription: string, +): Promise { + return Promise.race([ + promise, + (async () => { + await sleep(timeout); + throw new Error(`${promiseDescription} timed out after ${timeout}ms`); + })(), + ]); +} + +export { + promiseAll, + promiseFilter, + ignorePromiseRejections, + promiseWithTimeout, +};