- 01Setting Up (What You'll Need)
- 02Creating Variables
- 03Conditionals (if Statements)
- 04Loops (for Statements)
- 05Creating Functions
- 06Working with Arrays
- 07Working with Objects
- 08Loops (while Statements)
- 09Working with Strings
- 10Using Classes (Object-Oriented Programming)
- 11Error Handling (try...catch)
- 12Destructuring and the Spread Syntax
- 13Async Code (Promise / async & await)
- 14Advanced Array Methods (filter and reduce)
- 15The switch Statement
- 16The Ternary Operator
- 17Inheritance (Extending Classes)
- 18How to Write Comments
- 19Logical Operators (AND, OR, NOT)
- 20Constants (Making Read-Only Values with const)
- 21Splitting and Joining Strings (split and join)
- 22Null-Safe Syntax (?? and ?.)
- 23Searching Arrays and Collections (includes and find)
- 24Transforming Arrays with map
- 25Two-Dimensional Arrays (Table-Shaped Data)
- 26Building a Custom Error Class
- 27Default Arguments (Setting Initial Values for Parameters)
- 28Using Set (Collections)
- 29Verifying Correctness with assert (Your First Step Into Testing)
- 30Higher-Order Functions (Passing a Function as an Argument)
- 31Stacks and Queues (Basic Data Structures)
- 32The Basics of Type Conversion (Casting)
- 33Introduction to Regular Expressions (Pattern Matching)
- 34The Binary Search Algorithm
- 35Building a Caesar Cipher (a Letter-Shifting Cipher)
- 36Understanding How Bubble Sort Works
- 37Building and Displaying Dates (Basic Year/Month/Day Operations)
- 38Writing Several Unit Tests Together (Multiple Test Cases)
- 39Speeding Up Calculations with Memoization (Caching)
- 40Normalizing Strings (trim and Case Unification)
- 41The Difference Between Shallow Copy and Deep Copy
- 42The Basics of Enums (Enumerated Types)
- 43Flattening Arrays (flatten)
- 44Reversing a String and Checking for a Palindrome
- 45Pairing Up Two Arrays (a zip Operation)
- 46Rounding Numbers (floor, ceil, and round)
- 47Multi-Line Strings (Template Literals)
- 48Returning Multiple Values from a Function (Array Destructuring)
- 49Finding the GCD and LCM (the Euclidean Algorithm)
- 50Formatting Numbers (Digit Alignment and Decimal Precision)
- 51Cleanup Processing with try/catch/finally
- 52Writing Type-Agnostic, General-Purpose Functions
- 53The Basics of Map (an Object for Key-Value Pairs)
- 54Generating Random Numbers
- 55Bitwise Operations (AND, OR, XOR, and Shift Operations)
- 56Using static (Static Class Properties and Methods)
- 57Waiting a Fixed Amount of Time (setTimeout and await)
- 58Watch Out for Floating-Point Rounding Error
- 59Transforming and Flattening at Once with flatMap()
- 60FizzBuzz (the Classic Practice Problem)
- 61Checking Whether a Number Is Prime
- 62Set Operations with Set (Union, Intersection, and Difference)
- 63Converting Number Bases (Binary and Hexadecimal)
- 64Checking That Brackets Match (an Application of Stacks)
- 65Checking for an Anagram
- 66Checking Whether a Year Is a Leap Year
- 67Converting Temperature (Celsius ⇄ Fahrenheit)
- 68Finding the Prime Factorization
- 69[Applied] Build a Simple To-Do List Tool
Async Code (Promise / async & await)
In this lesson you'll learn the basics of asynchronous code in JavaScript (Promises and async/await) so you can correctly handle time-consuming operations like network requests. This is for people searching "JavaScript async await usage" or "JavaScript what is a Promise."
When dealing with time-consuming operations (such as network requests), JavaScript uses a mechanism called a Promise. Inside a function marked async, you can use await to "wait" until a Promise's result comes back. It's a mechanism for expressing processing with a time delay, like "waiting for food to arrive after placing an order." If you don't use await, the next line of code runs without waiting for the operation to finish.
The sample code builds a wait function using a Promise that waits for a given amount of time, and inside the main function it waits 300 milliseconds with await wait(300). Run it, and you'll see the second line appear only after a short delay. Notice too that the call to main() is also preceded by await, so the program waits for it to finish before moving on.
A common beginner stumbling block is trying to use await outside of a function, or inside a function that isn't marked async, which causes an error. Remember the rule: await can only be used inside an async function. Another common mistake is trying to use a result before the async operation has finished, resulting in an unexpected undefined.
In real development, all kinds of communication and waiting operations — fetching data from an API, reading a file, running something after a delay — are written using Promises and async/await. When building a web app that involves API communication, this way of thinking about async code is essential foundational knowledge you can't avoid.
When you want to run several async operations at once and wait for all of them to finish, you use Promise.all([task1, task2]). It's faster than awaiting each one in sequence, and it's a common technique when you want to run multiple API requests in parallel.
If an error occurs in an awaited operation, you can catch it with an ordinary try...catch. Being able to handle errors for both async and sync code with the same syntax is one reason async/await is preferred over writing Promises directly.
💡 Anything passed to console.log() appears in the output below.
