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>
251 lines
7.0 KiB
Markdown
251 lines
7.0 KiB
Markdown
---
|
|
id: javascript-async-await
|
|
title: "JavaScript Async Await"
|
|
category: "Frontend"
|
|
status: "draft"
|
|
verification_status: "conceptual"
|
|
canonical_id: ""
|
|
aliases: ["async await", "await keyword", "async function", "try catch await", "Promise.all"]
|
|
duplicate_of: ""
|
|
source_trust_level: "B"
|
|
confidence_score: 0.89
|
|
created_at: 2026-06-23
|
|
updated_at: 2026-06-23
|
|
review_reason: ""
|
|
merge_history: []
|
|
tags: ["javascript", "js", "web", "frontend", "w3schools", "async-await", "promises"]
|
|
raw_sources: ["https://www.w3schools.com/js/js_async_await.asp"]
|
|
applied_in: []
|
|
github_commit: ""
|
|
---
|
|
|
|
# [[JavaScript Async Await]]
|
|
|
|
## 🎯 한 줄 통찰 (One-line insight)
|
|
`async` and `await` make promises easier by letting you write asynchronous code like normal step-by-step code, with familiar `try...catch` error handling. [S1]
|
|
|
|
## 🧠 핵심 개념 (Core concepts)
|
|
- **They simplify promises** — `async` and `await` make promises easier by letting code be written like normal step-by-step code. [S1]
|
|
- **`async` returns a promise** — an `async` function returns a promise; its return value is what the promise resolves to. [S1]
|
|
- **`await` unwraps a promise** — `await` pauses inside the async function until the awaited promise resolves, giving back the value directly. [S1]
|
|
- **Error handling with try...catch** — wrap awaited calls in `try { ... } catch (error) { ... }` to handle rejections. [S1]
|
|
- **Sequential vs parallel** — awaiting one call after another runs them in sequence; `Promise.all([...])` runs them together and awaits all. [S1]
|
|
|
|
## 🧩 추출된 패턴 (Extracted patterns)
|
|
- **Linearize a promise chain** — replace `.then()` chaining with successive `await` calls inside an `async function`. [S1]
|
|
- **try/catch around await** — catch a rejected promise (e.g. `await fail()`) like a synchronous error. [S1]
|
|
- **Promise.all for parallelism** — start promises first, then `await Promise.all([p1, p2])` to collect results together. [S1]
|
|
|
|
## 📖 세부 내용 (Details)
|
|
**Promises approach (before)**
|
|
[S1]
|
|
```javascript
|
|
// Three functions to run in steps
|
|
function step1() {
|
|
return Promise.resolve("A");
|
|
}
|
|
function step2(value) {
|
|
return Promise.resolve(value + "B");
|
|
}
|
|
function step3(value) {
|
|
return Promise.resolve(value + "C");
|
|
}
|
|
|
|
// Run the three functions in steps
|
|
step1()
|
|
.then(function(value) {
|
|
return step2(value);
|
|
})
|
|
.then(function(value) {
|
|
return step3(value);
|
|
})
|
|
.then(function(value) {
|
|
myDisplayer(value);
|
|
});
|
|
```
|
|
|
|
**async/await approach (after)**
|
|
The same logic reads like sequential code: [S1]
|
|
```javascript
|
|
// Function to run the three functions in steps
|
|
async function run() {
|
|
let v1 = await step1();
|
|
let v2 = await step2(v1);
|
|
let v3 = await step3(v2);
|
|
myDisplayer(v3);
|
|
}
|
|
|
|
run();
|
|
```
|
|
|
|
**The async keyword**
|
|
An `async` function returns a promise: [S1]
|
|
```javascript
|
|
async function myFunction() {
|
|
return "Hello";
|
|
}
|
|
myFunction().then(
|
|
function(value) {myDisplayer(value);}
|
|
);
|
|
```
|
|
|
|
**The await keyword**
|
|
`await` waits for a promise and returns its resolved value: [S1]
|
|
```javascript
|
|
function step1() {
|
|
return Promise.resolve("A");
|
|
}
|
|
|
|
async function run() {
|
|
let value = await step1();
|
|
myDisplayer(value);
|
|
}
|
|
|
|
run();
|
|
```
|
|
|
|
**Error handling with try...catch**
|
|
[S1]
|
|
```javascript
|
|
function fail() {
|
|
return Promise.reject("Failed");
|
|
}
|
|
|
|
async function run() {
|
|
try {
|
|
let value = await fail();
|
|
console.log(value);
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
run();
|
|
```
|
|
|
|
**Sequential execution**
|
|
[S1]
|
|
```javascript
|
|
async function run() {
|
|
let a = await step1();
|
|
let b = await step2();
|
|
console.log(a, b);
|
|
}
|
|
```
|
|
|
|
**Parallel with Promise.all()**
|
|
Start the promises first, then await them together: [S1]
|
|
```javascript
|
|
async function run() {
|
|
let p1 = step1();
|
|
let p2 = step2();
|
|
let values = await Promise.all([p1, p2]);
|
|
console.log(values);
|
|
}
|
|
```
|
|
|
|
**fetch() with async/await**
|
|
[S1]
|
|
```javascript
|
|
async function loadData() {
|
|
try {
|
|
let response = await fetch("data.json");
|
|
let data = await response.json();
|
|
console.log(data);
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
loadData();
|
|
```
|
|
|
|
**Basic syntax with a Promise**
|
|
Awaiting a constructed promise: [S1]
|
|
```javascript
|
|
async function myDisplay() {
|
|
let myPromise = new Promise(function(resolve, reject) {
|
|
resolve("I love You !!");
|
|
});
|
|
document.getElementById("demo").innerHTML = await myPromise;
|
|
}
|
|
|
|
myDisplay();
|
|
```
|
|
Without the reject parameter: [S1]
|
|
```javascript
|
|
async function myDisplay() {
|
|
let myPromise = new Promise(function(resolve) {
|
|
resolve("I love You !!");
|
|
});
|
|
document.getElementById("demo").innerHTML = await myPromise;
|
|
}
|
|
|
|
myDisplay();
|
|
```
|
|
|
|
**Waiting for a timeout**
|
|
[S1]
|
|
```javascript
|
|
async function myDisplay() {
|
|
let myPromise = new Promise(function(resolve) {
|
|
setTimeout(function() {resolve("I love You !!");}, 3000);
|
|
});
|
|
document.getElementById("demo").innerHTML = await myPromise;
|
|
}
|
|
|
|
myDisplay();
|
|
```
|
|
|
|
## 🛠️ 적용 사례 (Applied in summary)
|
|
The page's snippets — the `run()` function linearizing step1/step2/step3, the `try...catch` around `await fail()`, and `await Promise.all([p1, p2])` — are the canonical applied examples. No external project/commit applications found in the source.
|
|
|
|
## 💻 코드 패턴 (Code patterns)
|
|
Linearize a chain with await (language: JavaScript):
|
|
```javascript
|
|
async function run() {
|
|
let v1 = await step1();
|
|
let v2 = await step2(v1);
|
|
let v3 = await step3(v2);
|
|
myDisplayer(v3);
|
|
}
|
|
|
|
run();
|
|
```
|
|
Error handling and parallelism:
|
|
```javascript
|
|
async function run() {
|
|
try {
|
|
let values = await Promise.all([step1(), step2()]);
|
|
console.log(values);
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
}
|
|
```
|
|
|
|
## ⚖️ 비교 및 선택 기준 (Comparison & decision criteria)
|
|
- **Promise `.then()` chaining** — explicit, but reads as nested handler functions and requires returning each step. [S1]
|
|
- **async/await** — the same logic written like normal step-by-step code, with `try...catch` for errors; recommended for readability. Use sequential `await` for dependent steps and `Promise.all` for independent ones. [S1]
|
|
|
|
## ⚖️ 모순 및 업데이트 (Contradictions & updates)
|
|
No contradictions found in the source.
|
|
|
|
## ✅ 검증 상태 및 신뢰도
|
|
- **상태:** draft
|
|
- **검증 단계:** conceptual (실제 적용 사례 발견 시 applied/validated로 승격 가능)
|
|
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
|
|
- **신뢰 점수:** 0.89
|
|
- **중복 검사 결과:** 신규 생성 (New discovery)
|
|
|
|
## 🔗 지식 그래프 (Knowledge Graph)
|
|
- **상위/루트:** [[JavaScript Tutorial]]
|
|
- **관련 개념:** [[JavaScript Promise]], [[JavaScript Async Fetch]], [[JavaScript Async Callbacks]], [[JavaScript Async Debug]]
|
|
- **참조 맥락:** The syntactic sugar over promises that most modern async data-fetching code uses.
|
|
|
|
## 📚 출처 (Sources)
|
|
- [S1] W3Schools — JavaScript Async Await — https://www.w3schools.com/js/js_async_await.asp
|
|
|
|
## 📝 변경 이력 (Change history)
|
|
- 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Async Await" page (Astra wiki-curation, P-Reinforce v3.1 format).
|