diff --git a/client/lib/wpcom-xhr-wrapper/index.js b/client/lib/wpcom-xhr-wrapper/index.js index a262cdd594874..3977761a27906 100644 --- a/client/lib/wpcom-xhr-wrapper/index.js +++ b/client/lib/wpcom-xhr-wrapper/index.js @@ -8,15 +8,18 @@ import debugModule from 'debug'; */ const debug = debugModule( 'calypso:wpcom-xhr-wrapper' ); -export default function ( params, callback ) { - return import( /* webpackChunkName: "wpcom-xhr-request" */ 'wpcom-xhr-request' ).then( ( xhr ) => - xhr.default( params, function ( error, response, headers ) { - if ( error && error.name === 'InvalidTokenError' ) { - debug( 'Invalid token error detected, authorisation probably revoked - logging out' ); - require( 'lib/user/utils' ).logout(); - } +export default async function ( params, callback ) { + const xhr = ( await import( /* webpackChunkName: "wpcom-xhr-request" */ 'wpcom-xhr-request' ) ) + .default; + const userUtils = ( await import( /* webpackChunkName: "lib-user-utils" */ 'lib/user/utils' ) ) + .default; - callback( error, response, headers ); - } ) - ); + return xhr( params, function ( error, response, headers ) { + if ( error && error.name === 'InvalidTokenError' ) { + debug( 'Invalid token error detected, authorisation probably revoked - logging out' ); + userUtils.logout(); + } + + callback( error, response, headers ); + } ); }