Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    JSJavaScript

    Getting started

    • What is JavaScript?
    • Variables: let, const and var
    • Data types
    • Type conversion

    Operators and strings

    • Arithmetic and assignment
    • Comparison and logic
    • Strings and template literals
    • String methods
    • Numbers and Math

    Control flow

    • Conditions: if and else
    • switch and the ternary operator
    • Loops: for and while
    • Challenge: FizzBuzz

    Functions

    • Functions
    • Arrow functions
    • Parameters: default, rest and spread
    • Scope and closures
    • Callbacks and higher-order functions

    Arrays

    • Array basics
    • map, filter and find
    • reduce and sorting
    • Destructuring

    Objects

    • Object basics
    • Copies and references
    • Methods and this
    • JSON and optional chaining

    Classes and OOP

    • Classes
    • Inheritance

    JavaScript in the page

    • Controlling the page (DOM)
    • Creating elements
    • Events
    • Forms and validation
    • Saving in the browser (localStorage)

    Errors and async

    • Handling errors: try and catch
    • Timers and the event loop
    • Promises
    • async and await
    • Fetching data from an API

    Practical tools and techniques

    • Searching with regular expressions
    • Cancellation and stale results
    • Testing and debugging JavaScript

    Modern JavaScript and a project

    • Map and Set
    • Dates and localised formatting
    • Modules: import and export
    • Project: a complete to-do app
    JavaScript

    Practical tools and techniques · Lesson 40 of 45

    Cancellation and stale results

    AbortController and search result races.

    8 minutes1 games

    When a user starts another search before the first finishes, an older response can overwrite the newer result. AbortController provides a signal to pass to a request; abort requests cancellation. A request sequence number also helps ensure only the latest result is applied.

    JavaScript
    const controller = new AbortController()
    controller.signal.addEventListener("abort", () => {
      console.log("Cancelled")
    }, { once: true })
    console.log(controller.signal.aborted)
    controller.abort()
    console.log(controller.signal.aborted)
    JavaScript
    let controller
    let latest = 0
    async function search(query) {
      controller?.abort()
      controller = new AbortController()
      const id = ++latest
      try {
        const res = await fetch(`/api/search?q=${encodeURIComponent(query)}`, {signal: controller.signal})
        if (!res.ok) throw new Error(`HTTP ${res.status}`)
        const data = await res.json()
        if (id === latest) console.log(data)
      } catch (err) {
        if (err.name !== "AbortError" && id === latest) console.error(err)
      }
    }

    Integration example for an app providing /api/search; this endpoint is not supplied by the lesson editor.

    First search

    New search aborts previous

    Check request id

    Apply only the latest result

    Important

    Cancellation does not roll back work the server has already performed. Do not reuse an aborted signal for a new request.

    Interview question

    Does debounce alone prevent stale responses?

    Exercise

    Create and abort an AbortController, then print signal.aborted.

    Expected output: true

    JavaScript

    Learn more from

    • javascript.info: fetch-abortjavascript.info
    • MDN: further readingdeveloper.mozilla.org

    Played 0 of 1 games

    Sign in to save your progress and earn XP.

    PreviousSearching with regular expressionsNextTesting and debugging JavaScript
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.