Reference Guide
Table of Contents
1. Asynchronous JavaScript Fundamentals
Synchronous vs Asynchronous
console.log('First');
console.log('Second');
console.log('Third');
// Output: First, Second, Third (in order)The Event Loop
Common Async Operations
2. Callbacks
What is a Callback?
Async Callbacks
Error-First Callback Pattern (Node.js)
Callback Hell
3. Promises
What is a Promise?
Creating a Promise
Example with Timeout
Consuming Promises
Promise Chaining
Promise Static Methods
Method
Returns
Rejects When
4. Async/Await
The Modern Way to Handle Async Code
The async Keyword
The await Keyword
Error Handling with Try/Catch
Parallel vs Sequential
Converting Promises to Async/Await
5. Fetch API
Basic Syntax
GET Request
POST Request
PUT Request (Update)
PATCH Request (Partial Update)
DELETE Request
Response Object
Request Headers
Fetch Options
Aborting Requests
CRITICAL: Fetch Doesn't Reject on HTTP Errors!
6. AJAX & HTTP
What is AJAX?
HTTP Request/Response Cycle
HTTP Status Codes
HTTP Methods (CRUD)
Operation
Method
Purpose
RESTful API Conventions
7. Working with JSON
JSON Syntax
JSON vs JavaScript Object
JSON.stringify() - Object to JSON String
JSON.parse() - JSON String to Object
JSON with Fetch
Important: JSON Limitations
8. Practical Patterns
Complete API Request Pattern
Loading States
Debouncing (Search Input)
Retry Logic
Caching
Error Handling by Status Code
9. Best Practices
1. Always Handle Errors
2. Check response.ok
3. Use JSON.stringify() for POST/PUT
4. Set Content-Type Header
5. Show Loading States
6. Use Promise.all() for Parallel Requests
7. Debounce Search Inputs
8. Never Expose API Keys in Frontend
9. Validate User Input
10. Use Appropriate HTTP Methods
10. Common Mistakes
1. Forgetting await
2. Not Checking response.ok
3. Forgetting JSON.stringify()
4. Sequential Instead of Parallel
5. Not Handling Errors
6. Using .then() with async/await
7. Not Disabling Buttons During Requests
Quick Reference
Fetch Template
CRUD Operations
Promise Methods
Practice APIs
Last updated