Skip to content

Instantly share code, notes, and snippets.

@hachibu
Last active October 6, 2022 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hachibu/19eca0b39b2763e86e85a28cabe3fb0d to your computer and use it in GitHub Desktop.
Save hachibu/19eca0b39b2763e86e85a28cabe3fb0d to your computer and use it in GitHub Desktop.
// 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