9609c04755
W3Schools 튜토리얼을 P-Reinforce v3.1 포맷으로 위키화(영어 본문, 한/영 섹션 헤더). - Topic_HTML: 59문서 (튜토리얼+예제, 레퍼런스/메타 제외) - Topic_CSS: 190문서 (메인 + Advanced/Flexbox/Grid/RWD 전체) - Topic_JavaScript: 120문서 (코어 언어; Temporal/DOM상세/BOM/WebAPI/AJAX/jQuery/Graphics 등은 후속) 각 폴더 00_INDEX.md(MOC) 포함. 코드 verbatim, 미확인분은 "Not found in source" 표기. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
197 lines
6.7 KiB
Markdown
197 lines
6.7 KiB
Markdown
---
|
|
id: javascript-async-debug
|
|
title: "JavaScript Async Debug"
|
|
category: "Frontend"
|
|
status: "draft"
|
|
verification_status: "conceptual"
|
|
canonical_id: ""
|
|
aliases: ["Debugging async", "Debug fetch", "Promise debugging", "response.ok debugging", "Network tab"]
|
|
duplicate_of: ""
|
|
source_trust_level: "B"
|
|
confidence_score: 0.87
|
|
created_at: 2026-06-23
|
|
updated_at: 2026-06-23
|
|
review_reason: ""
|
|
merge_history: []
|
|
tags: ["javascript", "js", "web", "frontend", "w3schools", "debugging", "asynchronous", "fetch"]
|
|
raw_sources: ["https://www.w3schools.com/js/js_async_debug.asp"]
|
|
applied_in: []
|
|
github_commit: ""
|
|
---
|
|
|
|
# [[JavaScript Async Debug]]
|
|
|
|
## 🎯 한 줄 통찰 (One-line insight)
|
|
Async bugs are hard because the code runs later — debug them by handling errors early, checking `response.ok`, logging between steps, and using the Network tab. [S1]
|
|
|
|
## 🧠 핵심 개념 (Core concepts)
|
|
- **Async bugs run later** — async code often fails after your function has already returned, so it feels like nothing happened. [S1]
|
|
- **Handle rejections early** — unhandled promise rejections confuse beginners; handle errors early and clearly. [S1]
|
|
- **Fetch does not reject on HTTP errors** — fetch does not reject on errors like 404; you must check `response.ok`. [S1]
|
|
- **Logging a promise ≠ logging data** — log the response and status first; logging a promise is not the same as logging the data. [S1]
|
|
- **Use the Network tab** — it shows every request and is the fastest way to debug fetch problems. [S1]
|
|
- **Forgetting await is the top mistake** — it is the most common beginner mistake. [S1]
|
|
- **Promises can fail anywhere** — add logs between steps to find where a chain fails. [S1]
|
|
|
|
## 🧩 추출된 패턴 (Extracted patterns)
|
|
- **Wrap awaits in try/catch** — catch failures around awaited code. [S1]
|
|
- **Guard with response.ok / status** — check and short-circuit on HTTP errors before reading the body. [S1]
|
|
- **Log response details first** — log `response.status` and headers before assuming JSON. [S1]
|
|
- **Log between chain steps** — add `console.log` inside each `.then()`/`.catch()` to locate the failing step. [S1]
|
|
|
|
## 📖 세부 내용 (Details)
|
|
**Why async is hard**
|
|
Async bugs are difficult because the code runs later. Async code often fails after your function has already returned, which makes it feel like nothing happened. [S1]
|
|
|
|
**Missing error handling**
|
|
The unhandled version (with a trailing synchronous log running before the async work resolves): [S1]
|
|
```javascript
|
|
async function loadData() {
|
|
let response = await fetch("missing.json");
|
|
let data = await response.json();
|
|
console.log(data);
|
|
}
|
|
|
|
loadData();
|
|
console.log("Done");
|
|
```
|
|
|
|
**Add try/catch**
|
|
Handle errors early and clearly: [S1]
|
|
```javascript
|
|
async function loadData() {
|
|
try {
|
|
let response = await fetch("missing.json");
|
|
let data = await response.json();
|
|
console.log(data);
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
}
|
|
```
|
|
|
|
**Check response.ok**
|
|
Fetch does not reject on HTTP errors like 404, so check `response.ok`: [S1]
|
|
```javascript
|
|
async function loadData() {
|
|
try {
|
|
let response = await fetch("missing.json");
|
|
|
|
if (!response.ok) {
|
|
console.log("HTTP Error:", response.status);
|
|
return;
|
|
}
|
|
|
|
let data = await response.json();
|
|
console.log(data);
|
|
} catch (error) {
|
|
console.log("Network error");
|
|
}
|
|
}
|
|
```
|
|
|
|
**Log response details first**
|
|
Logging a promise is not the same as logging the data — log the response and status first: [S1]
|
|
```javascript
|
|
async function loadData() {
|
|
let response = await fetch("data.json");
|
|
console.log(response.status);
|
|
console.log(response.headers.get("content-type"));
|
|
}
|
|
```
|
|
|
|
**Forgetting await (incorrect)**
|
|
Forgetting `await` is the most common beginner mistake — this logs a promise: [S1]
|
|
```javascript
|
|
async function loadData() {
|
|
let response = await fetch("data.json");
|
|
let data = response.json();
|
|
console.log(data);
|
|
}
|
|
```
|
|
Correct version: [S1]
|
|
```javascript
|
|
async function loadData() {
|
|
let response = await fetch("data.json");
|
|
let data = await response.json();
|
|
console.log(data);
|
|
}
|
|
```
|
|
|
|
**Log between promise-chain steps**
|
|
Promises can fail anywhere in the chain; add logs between steps to find where it fails: [S1]
|
|
```javascript
|
|
fetch("data.json")
|
|
.then(function(response) {
|
|
console.log("Got response");
|
|
return response.json();
|
|
})
|
|
.then(function(data) {
|
|
console.log("Got data");
|
|
console.log(data);
|
|
})
|
|
.catch(function(error) {
|
|
console.log("Failed");
|
|
console.log(error);
|
|
});
|
|
```
|
|
|
|
**Debugging checklist**
|
|
[S1]
|
|
- Check the console for errors.
|
|
- Add try/catch around awaited code.
|
|
- Check `response.ok` and `response.status`.
|
|
- Use the Network tab (it shows every request).
|
|
- Use breakpoints on `await` lines.
|
|
|
|
## 🛠️ 적용 사례 (Applied in summary)
|
|
The page's snippets — the unhandled `loadData()` + `console.log("Done")`, the try/catch and `response.ok` guards, and the logged promise chain — are the canonical applied examples. No external project/commit applications found in the source.
|
|
|
|
## 💻 코드 패턴 (Code patterns)
|
|
Robust fetch with guard and logging (language: JavaScript):
|
|
```javascript
|
|
async function loadData() {
|
|
try {
|
|
let response = await fetch("missing.json");
|
|
|
|
if (!response.ok) {
|
|
console.log("HTTP Error:", response.status);
|
|
return;
|
|
}
|
|
|
|
let data = await response.json();
|
|
console.log(data);
|
|
} catch (error) {
|
|
console.log("Network error");
|
|
}
|
|
}
|
|
```
|
|
Log between chain steps to locate failures:
|
|
```javascript
|
|
fetch("data.json")
|
|
.then(function(response) { console.log("Got response"); return response.json(); })
|
|
.then(function(data) { console.log("Got data"); console.log(data); })
|
|
.catch(function(error) { console.log("Failed"); console.log(error); });
|
|
```
|
|
|
|
## ⚖️ 모순 및 업데이트 (Contradictions & updates)
|
|
No contradictions found in the source.
|
|
|
|
## ✅ 검증 상태 및 신뢰도
|
|
- **상태:** draft
|
|
- **검증 단계:** conceptual (실제 적용 사례 발견 시 applied/validated로 승격 가능)
|
|
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
|
|
- **신뢰 점수:** 0.87
|
|
- **중복 검사 결과:** 신규 생성 (New discovery)
|
|
|
|
## 🔗 지식 그래프 (Knowledge Graph)
|
|
- **상위/루트:** [[JavaScript Tutorial]]
|
|
- **관련 개념:** [[JavaScript Async Fetch]], [[JavaScript Async Await]], [[JavaScript Promise]], [[JavaScript Async Callbacks]]
|
|
- **참조 맥락:** The practical troubleshooting capstone of the asynchronous study path.
|
|
|
|
## 📚 출처 (Sources)
|
|
- [S1] W3Schools — JavaScript Async Debug — https://www.w3schools.com/js/js_async_debug.asp
|
|
|
|
## 📝 변경 이력 (Change history)
|
|
- 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Async Debug" page (Astra wiki-curation, P-Reinforce v3.1 format).
|