--- 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).