Skip to content

Commit

Permalink
Merge pull request #382 from P4-Games/fix/20240720-missing-file
Browse files Browse the repository at this point in the history
[fix] 🐛 add middleware to handle alpha data (#375)
  • Loading branch information
dappsar authored Jul 20, 2024
2 parents de72990 + ff2e96b commit 031b0df
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/services/middleware.js
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
}
}
}
}

0 comments on commit 031b0df

Please sign in to comment.