-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #382 from P4-Games/fix/20240720-missing-file
[fix] 🐛 add middleware to handle alpha data (#375)
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const cache = new Map() | ||
|
||
export const fetchWithRetry = async (key, requestFunc, retries = 5, delay = 2000) => { | ||
// Verifica si el resultado ya está en caché | ||
if (cache.has(key)) { | ||
console.log('get from cache: ', key) | ||
return cache.get(key) | ||
} | ||
|
||
console.log('NO from cache: ', key) | ||
for (let i = 0; i < retries; i++) { | ||
try { | ||
const result = await requestFunc() | ||
// Guarda el resultado en caché | ||
cache.set(key, result) | ||
console.log('save in cache:', key, result) | ||
return result | ||
} catch (error) { | ||
console.log('retry: ', key, i + 1) | ||
if (error.code === -32005 && i < retries - 1) { | ||
await new Promise((resolve) => setTimeout(resolve, delay)) | ||
} else { | ||
throw error | ||
} | ||
} | ||
} | ||
} |