Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Stop the Answer and the Bill

In this video, you added a Stop button to the club’s page and watched what it stopped. Aborting the page’s fetch() stopped the reply on the page, but the server kept reading the model’s reply to the end and paid for it. Then you made the route abort its own call to the model when the page’s connection closes.

Key Points

  • A member may want to stop a reply. Mei sent her first message, then spotted a mistake in it (primero should be primera) and wanted to stop the reply and fix her message.
  • The page aborts its fetch() with an AbortController. Send gets an id, and a Stop button in the form has type="button", so pressing it doesn’t send the form. The script finds both buttons and keeps a controller, so Stop works before any message. Each message turns Send off, so a second message can’t replace the controller Stop aborts. Each message also gets a new controller, since an aborted one stays aborted. The controller’s signal goes to fetch(), and a click on Stop aborts it.
  • The page catches the abort. An abort makes fetch() or the for await loop throw, so the request and the loop sit in a try block. The catch keeps the words already shown and adds [Stopped], or [Connection lost] for any other error, such as a dropped connection. A finally block turns Send back on.
  • The server logs what each call costs. After the turn count, the route logs the call’s cost with costInCents() from the starting project, and skips the line when a call reports no usage.
  • Stopping only the page doesn’t stop the model. In the video’s run, Mei pressed Stop after one second and the page showed [Stopped]. Almost four seconds after Stop, the server logged the session at two turns and a cost of 0.051 cents, for 81 output tokens. The model wrote the whole reply, and the club paid for it.
  • There are two requests. Stop aborted the page’s request to the club’s server and closed that connection. The server’s call to the provider is a second request, and nothing aborted it, so the route’s loop kept reading the reply to the end. Each write into the closed connection fails silently, so nothing stopped the loop.
  • The cost adds up. The club pays a fraction of a cent for every stopped reply. And the reply Mei never saw stays in the history, so every later request pays to send it again.
  • The route aborts its own call when the page leaves. Before it calls streamAnswer(), it makes an AbortController of its own. Its response to the page, res, has a close event, and res.on() listens for it, like .addEventListener() on the page. It fires when that connection closes, after a Stop, a closed tab or a lost network. It also fires after a normal finish, so the handler aborts only when the response hasn’t finished (res.writableFinished is false).
  • streamAnswer() gets the controller’s signal. It already passes that signal to the provider’s request, so aborting ends that request.
  • The stopped call ends at once. In the rerun, the server logged the aborted call and one turn right after Stop. The route’s catch logs it as a failure. That’s expected here, but a real feature would log a stop as a stop.
  • No cost line doesn’t mean free. There was no cost line, because the call ended before reporting any usage. The provider can still bill Mei’s message and anything written before the abort. But closing the request tells the provider to stop generating the rest.
  • The stopped reply stays out of the history. The history kept Mei’s message alone, at its estimate. Then Mei sent her fixed message, and in this run, the reply streamed in.

A Stop button has to end two requests. The page aborts its fetch(), and the server notices the closed connection and aborts its call to the model.

The Code

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 The club’s page now streams the partner’s reply, but once a member presses Send, nothing can stop it. In this video, you’ll add a Stop button that stops the bill as well as the words.

00:12 Mei sends her first message, then spots a mistake in it: primera, not primero, is the right word. She wants to stop the reply and fix her message.

00:24 You’ve cancelled a fetch before, with an AbortController, so that’s the natural first try. In index.html, give Send an id, and add a Stop button. Its type is button, so pressing it doesn’t send the form.

00:39 In chat.js, the script finds both buttons and keeps a controller. Each message turns Send off, so a second message can’t replace the controller Stop aborts.

00:50 Each message also gets a new controller, since an aborted one stays aborted. fetch gets its signal, and a click on Stop aborts it. An abort makes fetch or the for await loop throw, so both now sit in a try block.

01:06 The catch keeps the words already shown and adds Stopped. For any other error, such as a dropped connection, it adds Connection lost. And a finally block turns Send back on.

01:19 To see each call’s cost, the server now logs it after the turn count, with costInCents() from your starting project. It skips the line when a call reports no usage.

01:31 Mei sends her message, and after one second, she presses Stop. The page shows Stopped. Will the server log a cost for this reply? Your turn: pause the video and predict.

01:50 It will. Almost four seconds after Stop, the server logs the session at two turns, and a cost of 0.051 cents, for 81 output tokens. The model wrote the whole reply, and the club pays for it.

02:07 Stop aborted the page’s request to the club’s server and closed that connection. The server’s call to the provider is a second request, and nothing aborted it.

02:16 So the route’s loop kept reading the reply to the end. Each write into the closed connection fails silently, so nothing stopped the loop. The club pays a fraction of a cent for every stopped reply.

02:29 And the reply Mei never saw stays in the history, so every later request pays to send it again.

02:37 So the route has to notice when the page leaves. Before it calls streamAnswer(), it makes an AbortController of its own. Its response to the page, res, has a close event, and res.on() listens for it, like addEventListener() on the page.

02:52 It fires when that connection closes: Stop, a closed tab, a lost network. It also fires after a normal finish, so the handler aborts only when the response hasn’t finished.

03:05 Then streamAnswer() gets the controller’s signal. That’s the signal it already passes to the provider’s request, so aborting ends that request.

03:15 After a restart, Mei sends the same message and presses Stop after one second. This time, the server’s log answers at once. The call was aborted, and the session has one turn.

03:28 There’s no cost line, because the call ended before reporting any usage. That doesn’t make it free. The provider can still bill Mei’s message and anything written before the abort.

03:39 But closing the request tells the provider to stop generating the rest. The history keeps Mei’s message alone, at its estimate. Now Mei sends her fixed message, and in this run, the reply streams in.

03:55 So a Stop button has to end two requests. The page aborts its fetch. The server notices the closed connection and aborts its call to the model. Next comes Exercise: Relay and Stop an Answer.

04:10 You’ll stream a club member’s reply to the page, and let them stop it, all the way to the model.

Become a Member to join the conversation.