From 8f1cb74e4e8cd604c640646ca4cbf9c266ed1902 Mon Sep 17 00:00:00 2001 From: alistair3149 Date: Sat, 16 Nov 2024 02:19:58 -0500 Subject: [PATCH] refactor: use AbortController for tabbertransclude fetch --- modules/ext.tabberNeue/Transclude.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/modules/ext.tabberNeue/Transclude.js b/modules/ext.tabberNeue/Transclude.js index b8e95db..8fd5d4f 100644 --- a/modules/ext.tabberNeue/Transclude.js +++ b/modules/ext.tabberNeue/Transclude.js @@ -48,15 +48,27 @@ class Transclude { * and rejects with an Error if there is an issue with the network request. */ async fetchDataFromUrl() { + // eslint-disable-next-line compat/compat + const controller = new AbortController(); + const timeoutId = setTimeout( () => controller.abort(), 5000 ); try { // eslint-disable-next-line n/no-unsupported-features/node-builtins - const response = await fetch( this.url, { method: 'GET', timeout: 5000, credentials: 'same-origin' } ); + const response = await fetch( this.url, { + method: 'GET', + credentials: 'same-origin', + signal: controller.signal + } ); + clearTimeout( timeoutId ); if ( !response.ok ) { - throw new Error( `Network response was not ok: ${ response.status } - ${ response.statusText }` ); + throw new Error( `[TabberNeue] Network response was not ok: ${ response.status } - ${ response.statusText }` ); } return Promise.resolve( response.text() ); } catch ( error ) { - return Promise.reject( `[TabberNeue] Error fetching data from URL: ${ this.url }`, error ); + if ( error.name === 'AbortError' ) { + return Promise.reject( new Error( '[TabberNeue] Request timed out after 5000ms' ) ); + } else { + return Promise.reject( new Error( `[TabberNeue] Error fetching data from URL: ${ this.url } - ${ error }` ) ); + } } }