In this video, you added one bounded retry loop to ask(), so a rate limit or a failing server costs the tea importer’s batch a short wait instead of an email, and a failure that retries can’t fix isn’t sent again.
Key Points
- Backoff, the cap and
Retry-After: when a request fails for a moment, wait and send it again, with each wait longer than the last, often twice as long: one second, then two, then four. A cap on the attempts means you give up in the end. When the server sends aRetry-Afterheader, its number of seconds replaces your own wait. - The stand-in server: this video’s failures come from a small local stand-in server that forwards real requests to the provider and fails the others with messages of its own, because you can’t make a provider fail on cue. It’s only for the recording, so you don’t need to build it.
- Without a loop, one
429stops the batch. With a429on the second email, Priya’s draft arrived, then the script stopped with an uncaught error that saidretryable: true, which means another try can help, andretryAfter: 2, the header’s two seconds. Marcus’s email got no draft, and Hannah’s was never sent. - The retry lives inside
ask(), around the provider’ssend(), not around each call toask(), so every feature that callsask()gets it.sendWithRetries()triessend()up toMAX_ATTEMPTS, four times. A reply comes straight back. An error that isn’t retryable, or a failure on the last attempt, is thrown on to the caller. Otherwise, the loop works out the wait,retryAfteror one second doubling with each attempt, prints a line, and waits.wait()wrapssetTimeout()in a promise, soawaitpauses the loop. - A wait over a minute is thrown on too.
MAX_WAIT_SECONDScaps how long the loop will wait, because reporting that email beats leaving the batch idle. - A billing failure, a wrong key and a call with no response are sent once and never retried, because their
retryableisn’ttrue. - The draft script reports a failure and moves on, unless the next email fails the same way. By the time an error reaches the script’s
catch,ask()has already retried it if it could. The script reports it withNot draftedand goes on to the next. But the problem might be the key or the account. SolastFailureremembers each failure’s message, and a draft clears it. If the next email fails with the same message, the script throws it, and the batch stops. - This rule replaces lesson 14’s stop by status. A
401now costs one more call before the batch stops, but the rule also catches failures that no list of statuses names. - With the loop, Marcus’s
429cost one retry after the two seconds the header asked for. Hannah’s email got a500every time: four attempts with waits of one, two and four seconds, seven seconds in all, thenNot drafted. The batch finished with two drafts out of three. - One loop, never two. This loop is for
fetch(), which sends each request once. A provider’s SDK already retries with backoff, so with an SDK you set itsmaxRetriesoption instead. A loop of yours around an SDK’s retries multiplies the requests: with an SDK’s three tries on each of this loop’s four attempts, one500would cost twelve requests.
The rule: retry only what the error marks as retryable. Wait the Retry-After seconds when there are some, and a doubling wait when there aren’t. Cap the attempts, then report the failure and move on.