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

    Errors and async · Lesson 36 of 45

    Promises

    A promise of a value to come: then, catch and Promise.all.

    9 minutes2 games

    A Promise represents a result that hasn't arrived yet (a data request, a file read). It has 3 states: pending, fulfilled (succeeded with a value) and rejected (failed with an error). Handle the value with .then() and failure with .catch().

    pending ⏳

    fulfilled ✓ → .then

    or rejected ✗ → .catch

    JavaScript
    function wait(ms, value) {
      return new Promise((resolve) => setTimeout(() => resolve(value), ms))
    }
    function fail(ms) {
      return new Promise((_, reject) => setTimeout(() => reject(new Error("Server is down")), ms))
    }
    
    wait(300, "data loaded")
      .then((msg) => {
        console.log(msg)
        return msg.length
      })
      .then((n) => console.log("length:", n))
    
    fail(400)
      .then(() => console.log("never"))
      .catch((err) => console.log("Caught:", err.message))
      .finally(() => console.log("cleanup"))

    Several promises at once

    JavaScript
    const wait = (ms, v) => new Promise((r) => setTimeout(() => r(v), ms))
    
    Promise.all([wait(300, "user"), wait(200, "posts"), wait(100, "likes")])
      .then((results) => console.log("all:", results))
    
    Promise.race([wait(300, "slow"), wait(100, "fast")])
      .then((winner) => console.log("race:", winner))

    Promise.all waits for all, running them in parallel (faster than one after another); race takes the first to finish.

    Important

    Promise.all rejects as soon as one fails. To get every result, including failures, use Promise.allSettled.

    Interview question

    What's the difference between Promise.all, allSettled, race and any?

    What's the output?

    Promise.resolve(2)
      .then((x) => x * 3)
      .then((x) => console.log(x))

    What will this code print?

    Quick question

    You want data from 3 APIs at the same time and need all of it. What do you use?

    Learn more from

    • javascript.info: Promisejavascript.info
    • javascript.info: Promise APIjavascript.info
    • MDN: Using promisesdeveloper.mozilla.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousTimers and the event loopNextasync and await
    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.