Read a Retry-After header

Turn delay-seconds or an HTTP date into milliseconds to wait, never negative.

JavaScript Version 1.0.0 · MIT · by Oveyon · .md

parse-a-retry-after-header.js
function retryAfterMs({ retryAfter, date }) { const seconds = Number(retryAfter); return Math.max(0, Number.isFinite(seconds) ? seconds * 1000 : Date.parse(retryAfter) - Date.parse(date)); }

Input & expected output

Input

{
  "retryAfter": "30",
  "date": "Wed, 28 Oct 2026 14:00:00 GMT"
}

Expected output

30000

The edge case

Input

{
  "retryAfter": "Wed, 28 Oct 2026 14:00:30 GMT",
  "date": "Wed, 28 Oct 2026 14:00:00 GMT"
}

Expected output

30000

Know the limits

  • An HTTP date is compared with the response’s own Date header, not your clock, so clock skew cannot shorten the wait. The lab accepts delay-seconds from 0 to 86,400.
  • Oveyon answers rate limiting with HTTP 429 and Retry-After. Wait at least this long, then retry with the same idempotency key.

The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.

Source & maintenance

Written by Oveyon for this edition of 1loc.dev; it is not part of the original collection. Copyright (c) 2026 Oveyon. MIT licensed.

The function above is the implementation the lab runs and the tests exercise. Read the license.

Local fixture scope · Node.js 22.23.2 · reviewed October 28, 2026. No external service is required.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.