Last active
October 6, 2022 14:12
-
-
Save hachibu/19eca0b39b2763e86e85a28cabe3fb0d to your computer and use it in GitHub Desktop.
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
// https://medium.com/star-gazers/improving-tail-latency-with-request-hedging-700c77cabeda | |
async function fetchHedged(url, { timeout }) { | |
let res; | |
try { | |
res = await fetch(url, { | |
signal: AbortSignal.timeout(timeout) | |
}) | |
} catch (error) { | |
if (error && error.code !== 'ABORT_ERR') { | |
throw error | |
} | |
res = await fetch(url, { | |
signal: AbortSignal.timeout(timeout) | |
}) | |
} | |
return res | |
} | |
(async() => { | |
const res = await fetchHedged( | |
"https://jsonplaceholder.typicode.com/todos/1", | |
{ timeout: 50 } | |
) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment