File tree Expand file tree Collapse file tree 1 file changed +8
-2
lines changed Expand file tree Collapse file tree 1 file changed +8
-2
lines changed Original file line number Diff line number Diff line change 1+ const dataCache : Record < string , any > = { } ;
2+
13export async function loadData (
24 apiUrl : string ,
35 maxRetries = 3 ,
@@ -7,6 +9,11 @@ export async function loadData(
79 throw new Error ( `No API URL provided` ) ;
810 }
911
12+ if ( dataCache [ apiUrl ] ) {
13+ console . log ( `Cache hit for: ${ apiUrl } ` ) ;
14+ return dataCache [ apiUrl ] ;
15+ }
16+
1017 for ( let attempt = 1 ; attempt <= maxRetries ; attempt ++ ) {
1118 try {
1219 console . log ( `Fetching data from: ${ apiUrl } (Attempt ${ attempt } )` ) ;
@@ -35,10 +42,9 @@ export async function loadData(
3542 console . log (
3643 `Received JSON object with ${ Object . keys ( data ) . length } keys`
3744 ) ;
38- } else {
39- console . log ( `Received JSON of type: ${ typeof data } ` ) ;
4045 }
4146
47+ dataCache [ apiUrl ] = data ;
4248 return data ;
4349 } catch ( error ) {
4450 console . error ( `Attempt ${ attempt } failed:` , error ) ;
You can’t perform that action at this time.
0 commit comments