Complete the async function:
๐ฆ Node.js-> Command Line
โจ The Prompt Phrase
async function fetchData() {
ย ย return awaitPromise .resolve('Hello!');
}
๐ป Code Preview
๐ฆ All-in-One Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Async/Await in JavaScript - Interactive Tutorial</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&family=Fira+Code:wght@400;500&display=swap" rel="stylesheet">
<style>
:root {
--bg-primary: #0a0e27;
--bg-secondary: #151932;
--bg-card: #1e2139;
--accent-purple: #a855f7;
--accent-blue: #3b82f6;
--accent-green: #10b981;
--accent-red: #ef4444;
--accent-yellow: #f59e0b;
--accent-pink: #ec4899;
--text-primary: #f8fafc;
--text-secondary: #94a3b8;
--gradient-1: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
--gradient-2: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
--gradient-3: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
--gradient-async: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
--shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Poppins', sans-serif;
background: var(--bg-primary);
color: var(--text-primary);
line-height: 1.6;
overflow-x: hidden;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
/* Hero Section */
.hero {
text-align: center;
padding: 100px 20px;
background: var(--gradient-async);
position: relative;
overflow: hidden;
}
.hero::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: url('data:image/svg+xml,<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"><circle cx="50" cy="50" r="2" fill="white" opacity="0.1"/></svg>');
animation: float 20s linear infinite;
}
@keyframes float {
from { transform: translateY(0); }
to { transform: translateY(-100px); }
}
.hero-content {
position: relative;
z-index: 1;
}
.command-display {
display: inline-block;
background: rgba(0, 0, 0, 0.3);
padding: 20px 30px;
border-radius: 15px;
font-family: 'Fira Code', monospace;
font-size: 1.1rem;
margin: 30px 0;
border: 2px solid rgba(255, 255, 255, 0.2);
animation: pulse 2s ease-in-out infinite;
max-width: 90%;
text-align: left;
}
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.02); }
}
.hero h1 {
font-size: 3rem;
font-weight: 700;
margin-bottom: 20px;
animation: slideDown 0.8s ease-out;
color: #1a202c;
}
.hero p {
font-size: 1.3rem;
opacity: 0.95;
animation: slideUp 0.8s ease-out;
color: #2d3748;
}
@keyframes slideDown {
from { transform: translateY(-50px); opacity: 0; }
to { transform: translateY(0); opacity: 1; }
}
@keyframes slideUp {
from { transform: translateY(50px); opacity: 0; }
to { transform: translateY(0); opacity: 1; }
}
/* Progress Bar */
.progress-container {
position: sticky;
top: 0;
width: 100%;
height: 5px;
background: var(--bg-secondary);
z-index: 1000;
}
.progress-bar {
height: 100%;
background: var(--gradient-3);
width: 0%;
transition: width 0.3s ease;
}
/* Section Styles */
.section {
margin: 60px 0;
animation: fadeIn 0.8s ease-out;
animation-fill-mode: both;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(30px); }
to { opacity: 1; transform: translateY(0); }
}
.section-title {
font-size: 2.5rem;
margin-bottom: 30px;
background: var(--gradient-3);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
display: inline-block;
}
.card {
background: var(--bg-card);
border-radius: 20px;
padding: 40px;
margin: 30px 0;
box-shadow: var(--shadow);
border: 1px solid rgba(255, 255, 255, 0.05);
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 25px 70px rgba(0, 0, 0, 0.4);
}
/* Code Block */
.code-block {
background: #1e1e1e;
border-radius: 15px;
padding: 25px;
margin: 20px 0;
position: relative;
overflow: hidden;
font-family: 'Fira Code', monospace;
}
.code-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
padding-bottom: 15px;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.code-lang {
color: var(--accent-green);
font-weight: 600;
}
.copy-btn {
background: var(--accent-purple);
color: white;
border: none;
padding: 8px 16px;
border-radius: 8px;
cursor: pointer;
font-size: 0.9rem;
transition: all 0.3s ease;
}
.copy-btn:hover {
background: var(--accent-blue);
transform: scale(1.05);
}
.copy-btn.copied {
background: var(--accent-green);
}
pre {
margin: 0;
overflow-x: auto;
}
code {
color: #e0e0e0;
font-size: 0.95rem;
line-height: 1.8;
}
.code-comment {
color: #6a9955;
}
.code-keyword {
color: #569cd6;
}
.code-string {
color: #ce9178;
}
.code-function {
color: #dcdcaa;
}
.code-number {
color: #b5cea8;
}
.code-variable {
color: #9cdcfe;
}
/* Steps */
.steps {
counter-reset: step-counter;
}
.step {
background: var(--bg-secondary);
border-radius: 15px;
padding: 30px;
margin: 20px 0;
position: relative;
padding-left: 80px;
transition: all 0.3s ease;
}
.step:hover {
background: var(--bg-card);
transform: translateX(10px);
}
.step::before {
counter-increment: step-counter;
content: counter(step-counter);
position: absolute;
left: 20px;
top: 50%;
transform: translateY(-50%);
width: 50px;
height: 50px;
background: var(--gradient-async);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.5rem;
font-weight: 700;
color: #1a202c;
}
.step h3 {
color: var(--accent-pink);
margin-bottom: 10px;
}
/* Interactive Demo */
.demo-container {
background: var(--bg-secondary);
border-radius: 20px;
padding: 40px;
margin: 30px 0;
}
.demo-output {
background: #1e1e1e;
border-radius: 15px;
padding: 20px;
margin: 20px 0;
min-height: 100px;
font-family: 'Fira Code', monospace;
color: var(--accent-green);
}
.demo-button {
background: var(--gradient-async);
color: #1a202c;
border: none;
padding: 15px 40px;
border-radius: 50px;
font-size: 1.1rem;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
display: inline-flex;
align-items: center;
gap: 10px;
margin: 10px;
}
.demo-button:hover {
transform: scale(1.05);
box-shadow: 0 10px 30px rgba(168, 237, 234, 0.4);
}
.loading-spinner {
display: inline-block;
width: 20px;
height: 20px;
border: 3px solid rgba(0,0,0,0.1);
border-top-color: #1a202c;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
/* Tabs */
.tabs {
display: flex;
gap: 10px;
margin-bottom: 20px;
flex-wrap: wrap;
}
.tab {
padding: 12px 24px;
background: var(--bg-secondary);
border: none;
border-radius: 10px;
color: var(--text-primary);
cursor: pointer;
transition: all 0.3s ease;
font-size: 1rem;
}
.tab:hover {
background: var(--bg-card);
}
.tab.active {
background: var(--gradient-async);
color: #1a202c;
}
.tab-content {
display: none;
}
.tab-content.active {
display: block;
animation: fadeIn 0.5s ease-out;
}
/* Accordion */
.accordion-header {
cursor: pointer;
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px;
background: var(--bg-secondary);
border-radius: 12px;
margin: 15px 0;
transition: all 0.3s ease;
}
.accordion-header:hover {
background: var(--bg-card);
}
.accordion-icon {
transition: transform 0.3s ease;
font-size: 1.2rem;
}
.accordion-icon.active {
transform: rotate(180deg);
}
.accordion-content {
max-height: 0;
overflow: hidden;
transition: max-height 0.3s ease;
}
.accordion-content.active {
max-height: 2000px;
}
.accordion-body {
padding: 20px;
background: var(--bg-secondary);
border-radius: 12px;
margin-top: 5px;
}
/* Tips */
.tip {
background: rgba(59, 130, 246, 0.1);
border-left: 4px solid var(--accent-blue);
padding: 20px;
border-radius: 10px;
margin: 20px 0;
}
.tip::before {
content: '๐ก ';
font-size: 1.5rem;
}
.warning {
background: rgba(239, 68, 68, 0.1);
border-left: 4px solid #ef4444;
padding: 20px;
border-radius: 10px;
margin: 20px 0;
}
.warning::before {
content: 'โ ๏ธ ';
font-size: 1.5rem;
}
.success {
background: rgba(16, 185, 129, 0.1);
border-left: 4px solid var(--accent-green);
padding: 20px;
border-radius: 10px;
margin: 20px 0;
}
.success::before {
content: 'โ
';
font-size: 1.5rem;
}
.info {
background: rgba(236, 72, 153, 0.1);
border-left: 4px solid var(--accent-pink);
padding: 20px;
border-radius: 10px;
margin: 20px 0;
}
.info::before {
content: '๐ ';
font-size: 1.5rem;
}
/* Quiz */
.quiz-container {
background: var(--bg-card);
border-radius: 20px;
padding: 40px;
margin: 30px 0;
}
.quiz-question {
margin: 30px 0;
}
.quiz-question h3 {
color: var(--accent-purple);
margin-bottom: 20px;
}
.quiz-options {
display: flex;
flex-direction: column;
gap: 15px;
}
.quiz-option {
background: var(--bg-secondary);
padding: 20px;
border-radius: 12px;
cursor: pointer;
transition: all 0.3s ease;
border: 2px solid transparent;
}
.quiz-option:hover {
border-color: var(--accent-blue);
transform: translateX(10px);
}
.quiz-option.correct {
border-color: var(--accent-green);
background: rgba(16, 185, 129, 0.1);
}
.quiz-option.incorrect {
border-color: #ef4444;
background: rgba(239, 68, 68, 0.1);
}
.quiz-feedback {
margin-top: 15px;
padding: 15px;
border-radius: 10px;
display: none;
}
.quiz-feedback.show {
display: block;
animation: slideIn 0.5s ease-out;
}
@keyframes slideIn {
from { opacity: 0; transform: translateX(-20px); }
to { opacity: 1; transform: translateX(0); }
}
.quiz-feedback.correct {
background: rgba(16, 185, 129, 0.1);
border-left: 4px solid var(--accent-green);
}
.quiz-feedback.incorrect {
background: rgba(239, 68, 68, 0.1);
border-left: 4px solid #ef4444;
}
/* Cheat Sheet */
.cheat-sheet {
background: var(--gradient-async);
border-radius: 20px;
padding: 40px;
margin: 30px 0;
}
.cheat-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
margin-top: 30px;
}
.cheat-item {
background: rgba(255, 255, 255, 0.2);
backdrop-filter: blur(10px);
padding: 20px;
border-radius: 15px;
border: 1px solid rgba(255, 255, 255, 0.3);
}
.cheat-item h4 {
color: #1a202c;
margin-bottom: 10px;
font-weight: 600;
}
.cheat-item code {
background: rgba(0, 0, 0, 0.2);
padding: 2px 8px;
border-radius: 5px;
font-size: 0.9rem;
color: #1a202c;
}
.badge {
display: inline-block;
padding: 5px 15px;
background: var(--gradient-2);
border-radius: 20px;
font-size: 0.85rem;
font-weight: 600;
margin: 5px;
}
.highlight {
color: var(--accent-pink);
font-weight: 600;
}
/* Footer */
.footer {
text-align: center;
padding: 40px 20px;
background: var(--bg-secondary);
margin-top: 80px;
}
.footer p {
color: var(--text-secondary);
margin: 10px 0;
}
/* Confetti */
.confetti {
position: fixed;
width: 10px;
height: 10px;
z-index: 9999;
}
@keyframes confetti-fall {
to {
transform: translateY(100vh) rotate(360deg);
opacity: 0;
}
}
/* Responsive */
@media (max-width: 768px) {
.hero h1 {
font-size: 2rem;
}
.command-display {
font-size: 0.9rem;
padding: 15px 20px;
}
.hero p {
font-size: 1rem;
}
.section-title {
font-size: 1.8rem;
}
.card {
padding: 25px;
}
.step {
padding-left: 70px;
}
.step::before {
width: 40px;
height: 40px;
font-size: 1.2rem;
}
}
.timeline {
position: relative;
padding: 20px 0;
}
.timeline-item {
display: flex;
gap: 20px;
margin: 20px 0;
align-items: center;
}
.timeline-dot {
width: 30px;
height: 30px;
background: var(--gradient-async);
border-radius: 50%;
flex-shrink: 0;
}
.timeline-content {
background: var(--bg-secondary);
padding: 15px 20px;
border-radius: 10px;
flex: 1;
}
</style>
</head>
<body>
<div class="progress-container">
<div class="progress-bar" id="progressBar"></div>
</div>
<div class="hero">
<div class="hero-content">
<h1>โณ Master Async/Await in JavaScript!</h1>
<div class="command-display">async function fetchData() {<br> return await Promise.resolve('Hello!');<br>}</div>
<p>Write asynchronous code that looks synchronous! ๐</p>
</div>
</div>
<div class="container">
<!-- What Is It Section -->
<section class="section" id="what-is-it">
<h2 class="section-title">๐ค What Is It?</h2>
<div class="card">
<p style="font-size: 1.2rem; margin-bottom: 20px;">
<span class="highlight">Async/await</span> is a modern way to handle asynchronous operations in JavaScript - making your code cleaner and easier to read! โจ
</p>
<p style="margin-bottom: 20px;">
Think of it like ordering food at a restaurant: Instead of standing at the counter waiting (blocking), you sit down, order, and the waiter brings your food when it's ready (non-blocking). Meanwhile, you can chat with friends! ๐
</p>
<div class="info">
<strong>In Simple Terms:</strong> <code>async/await</code> lets you write code that waits for things (like API calls or file reads) without freezing your entire program. It's like having a personal assistant who handles the waiting for you! ๐ฏ
</div>
<div style="margin-top: 30px;">
<h3 style="color: var(--accent-pink); margin-bottom: 15px;">Breaking It Down ๐</h3>
<ul style="margin-left: 30px; line-height: 2;">
<li><strong>async</strong>: Declares a function that returns a Promise</li>
<li><strong>await</strong>: Pauses execution until the Promise resolves</li>
<li><strong>Promise</strong>: Represents a value that will be available in the future</li>
</ul>
</div>
</div>
</section>
<!-- Why Use It Section -->
<section class="section" id="why-use-it">
<h2 class="section-title">โจ Why Use Async/Await?</h2>
<div class="card">
<div class="cheat-grid">
<div class="cheat-item">
<h4>๐ Readable Code</h4>
<p>Looks like regular synchronous code - much easier to understand!</p>
</div>
<div class="cheat-item">
<h4>๐ฏ Error Handling</h4>
<p>Use try/catch blocks just like synchronous code!</p>
</div>
<div class="cheat-item">
<h4>๐ No Callback Hell</h4>
<p>Avoid nested callbacks and "pyramid of doom"!</p>
</div>
<div class="cheat-item">
<h4>๐ Easy Debugging</h4>
<p>Stack traces are clearer and easier to follow!</p>
</div>
<div class="cheat-item">
<h4>๐จ Modern Standard</h4>
<p>The preferred way to handle async operations in modern JavaScript!</p>
</div>
<div class="cheat-item">
<h4>โก Better Performance</h4>
<p>Can easily run multiple async operations in parallel!</p>
</div>
</div>
</div>
</section>
<!-- How Does It Work Section -->
<section class="section" id="how-it-works">
<h2 class="section-title">โ๏ธ How Does It Work?</h2>
<div class="card">
<p style="margin-bottom: 30px;">Let's break down the code step by step! ๐ฌ</p>
<div class="steps">
<div class="step">
<h3>Step 1: The async Keyword</h3>
<div class="code-block">
<pre><code><span class="code-keyword">async function</span> <span class="code-function">fetchData</span>() {</code></pre>
</div>
<p>The <code>async</code> keyword before <code>function</code> tells JavaScript: "This function will handle asynchronous operations." It automatically wraps the return value in a Promise! ๐</p>
</div>
<div class="step">
<h3>Step 2: The await Keyword</h3>
<div class="code-block">
<pre><code> <span class="code-keyword">return await</span> Promise.<span class="code-function">resolve</span>(<span class="code-string">'Hello!'</span>);</code></pre>
</div>
<p><code>await</code> pauses the function execution until the Promise resolves. It's like saying "Wait for this to finish before moving on!" โธ๏ธ</p>
</div>
<div class="step">
<h3>Step 3: Promise.resolve()</h3>
<div class="code-block">
<pre><code>Promise.<span class="code-function">resolve</span>(<span class="code-string">'Hello!'</span>)</code></pre>
</div>
<p>Creates a Promise that immediately resolves with the value 'Hello!'. In real code, this could be an API call, database query, or file read! ๐ก</p>
</div>
<div class="step">
<h3>Step 4: Return the Result</h3>
<div class="code-block">
<pre><code><span class="code-keyword">return</span> <span class="code-keyword">await</span> ...</code></pre>
</div>
<p>Returns the resolved value ('Hello!') to whoever calls this function. The function itself returns a Promise! ๐ฏ</p>
</div>
</div>
<div class="success" style="margin-top: 30px;">
<strong>Key Point:</strong> An <code>async</code> function ALWAYS returns a Promise, even if you return a regular value!
</div>
</div>
</section>
<!-- Timeline Visualization -->
<section class="section" id="timeline">
<h2 class="section-title">โฑ๏ธ Execution Timeline</h2>
<div class="card">
<p style="margin-bottom: 20px;">Here's what happens when you call the function:</p>
<div class="timeline">
<div class="timeline-item">
<div class="timeline-dot"></div>
<div class="timeline-content">
<strong>1. Function Called</strong>
<p>fetchData() is invoked</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-dot"></div>
<div class="timeline-content">
<strong>2. Await Encountered</strong>
<p>Function pauses at 'await', waiting for Promise</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-dot"></div>
<div class="timeline-content">
<strong>3. Promise Resolves</strong>
<p>Promise.resolve('Hello!') completes immediately</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-dot"></div>
<div class="timeline-content">
<strong>4. Function Resumes</strong>
<p>Returns 'Hello!' wrapped in a Promise</p>
</div>
</div>
</div>
</div>
</section>
<!-- Interactive Demo -->
<section class="section" id="demo">
<h2 class="section-title">๐ฎ Interactive Demo</h2>
<div class="demo-container">
<h3 style="margin-bottom: 20px;">Try It Yourself! ๐ฏ</h3>
<p style="margin-bottom: 30px;">Click the buttons to see async/await in action!</p>
<div style="display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 20px;">
<button class="demo-button" onclick="runAsyncDemo()">
<span>โถ๏ธ</span>
<span>Run Async Function</span>
</button>
<button class="demo-button" onclick="runSlowDemo()">
<span>โฑ๏ธ</span>
<span>Simulate Slow Operation</span>
</button>
<button class="demo-button" onclick="runParallelDemo()">
<span>โก</span>
<span>Run in Parallel</span>
</button>
</div>
<div class="demo-output" id="demoOutput">
<div style="color: var(--text-secondary);">Click a button to see the output...</div>
</div>
</div>
</section>
<!-- Code Examples -->
<section class="section" id="examples">
<h2 class="section-title">๐ป Real-World Examples</h2>
<div class="card">
<div class="tabs">
<button class="tab active" onclick="switchTab('basic')">Basic Example</button>
<button class="tab" onclick="switchTab('api')">API Call</button>
<button class="tab" onclick="switchTab('error')">Error Handling</button>
<button class="tab" onclick="switchTab('parallel')">Parallel Execution</button>
</div>
<div id="basic" class="tab-content active">
<h3 style="color: var(--accent-pink); margin-bottom: 15px;">Basic Async/Await</h3>
<div class="code-block">
<div class="code-header">
<span class="code-lang">JavaScript</span>
<button class="copy-btn" onclick="copyCode(this, 'basic-code')">๐ Copy</button>
</div>
<pre><code id="basic-code"><span class="code-comment">// Define an async function</span>
<span class="code-keyword">async function</span> <span class="code-function">fetchData</span>() {
<span class="code-keyword">return await</span> Promise.<span class="code-function">resolve</span>(<span class="code-string">'Hello!'</span>);
}
<span class="code-comment">// Call the async function</span>
<span class="code-function">fetchData</span>().<span class="code-function">then</span>(<span class="code-variable">result</span> => {
console.<span class="code-function">log</span>(<span class="code-variable">result</span>); <span class="code-comment">// Output: Hello!</span>
});
<span class="code-comment">// Or use await in another async function</span>
<span class="code-keyword">async function</span> <span class="code-function">main</span>() {
<span class="code-keyword">const</span> <span class="code-variable">data</span> = <span class="code-keyword">await</span> <span class="code-function">fetchData</span>();
console.<span class="code-function">log</span>(<span class="code-variable">data</span>); <span class="code-comment">// Output: Hello!</span>
}
<span class="code-function">main</span>();</code></pre>
</div>
</div>
<div id="api" class="tab-content">
<h3 style="color: var(--accent-pink); margin-bottom: 15px;">Fetching Data from API</h3>
<div class="code-block">
<div class="code-header">
<span class="code-lang">JavaScript</span>
<button class="copy-btn" onclick="copyCode(this, 'api-code')">๐ Copy</button>
</div>
<pre><code id="api-code"><span class="code-comment">// Fetch data from an API</span>
<span class="code-keyword">async function</span> <span class="code-function">getUserData</span>(<span class="code-variable">userId</span>) {
<span class="code-comment">// Wait for the fetch to complete</span>
<span class="code-keyword">const</span> <span class="code-variable">response</span> = <span class="code-keyword">await</span> <span class="code-function">fetch</span>(<span class="code-string">`https://api.example.com/users/${userId}`</span>);
<span class="code-comment">// Wait for JSON parsing</span>
<span class="code-keyword">const</span> <span class="code-variable">data</span> = <span class="code-keyword">await</span> <span class="code-variable">response</span>.<span class="code-function">json</span>();
<span class="code-keyword">return</span> <span class="code-variable">data</span>;
}
<span class="code-comment">// Usage</span>
<span class="code-keyword">async function</span> <span class="code-function">displayUser</span>() {
<span class="code-keyword">const</span> <span class="code-variable">user</span> = <span class="code-keyword">await</span> <span class="code-function">getUserData</span>(<span class="code-number">123</span>);
console.<span class="code-function">log</span>(<span class="code-variable">user</span>.<span class="code-variable">name</span>);
}</code></pre>
</div>
</div>
<div id="error" class="tab-content">
<h3 style="color: var(--accent-pink); margin-bottom: 15px;">Error Handling with Try/Catch</h3>
<div class="code-block">
<div class="code-header">
<span class="code-lang">JavaScript</span>
<button class="copy-btn" onclick="copyCode(this, 'error-code')">๐ Copy</button>
</div>
<pre><code id="error-code"><span class="code-keyword">async function</span> <span class="code-function">fetchDataSafely</span>() {
<span class="code-keyword">try</span> {
<span class="code-comment">// Try to fetch data</span>
<span class="code-keyword">const</span> <span class="code-variable">response</span> = <span class="code-keyword">await</span> <span class="code-function">fetch</span>(<span class="code-string">'https://api.example.com/data'</span>);
<span class="code-keyword">if</span> (!<span class="code-variable">response</span>.<span class="code-variable">ok</span>) {
<span class="code-keyword">throw new</span> <span class="code-function">Error</span>(<span class="code-string">'Network response was not ok'</span>);
}
<span class="code-keyword">const</span> <span class="code-variable">data</span> = <span class="code-keyword">await</span> <span class="code-variable">response</span>.<span class="code-function">json</span>();
<span class="code-keyword">return</span> <span class="code-variable">data</span>;
} <span class="code-keyword">catch</span> (<span class="code-variable">error</span>) {
<span class="code-comment">// Handle any errors</span>
console.<span class="code-function">error</span>(<span class="code-string">'Error fetching data:'</span>, <span class="code-variable">error</span>);
<span class="code-keyword">return</span> <span class="code-keyword">null</span>;
}
}
<span class="code-comment">// Usage</span>
<span class="code-keyword">const</span> <span class="code-variable">data</span> = <span class="code-keyword">await</span> <span class="code-function">fetchDataSafely</span>();</code></pre>
</div>
</div>
<div id="parallel" class="tab-content">
<h3 style="color: var(--accent-pink); margin-bottom: 15px;">Running Operations in Parallel</h3>
<div class="code-block">
<div class="code-header">
<span class="code-lang">JavaScript</span>
<button class="copy-btn" onclick="copyCode(this, 'parallel-code')">๐ Copy</button>
</div>
<pre><code id="parallel-code"><span class="code-comment">// โ Sequential (slow) - waits for each one</span>
<span class="code-keyword">async function</span> <span class="code-function">sequential</span>() {
<span class="code-keyword">const</span> <span class="code-variable">user</span> = <span class="code-keyword">await</span> <span class="code-function">fetchUser</span>(); <span class="code-comment">// Wait 1 second</span>
<span class="code-keyword">const</span> <span class="code-variable">posts</span> = <span class="code-keyword">await</span> <span class="code-function">fetchPosts</span>(); <span class="code-comment">// Wait another 1 second</span>
<span class="code-keyword">return</span> { <span class="code-variable">user</span>, <span class="code-variable">posts</span> }; <span class="code-comment">// Total: 2 seconds</span>
}
<span class="code-comment">// โ
Parallel (fast) - runs simultaneously</span>
<span class="code-keyword">async function</span> <span class="code-function">parallel</span>() {
<span class="code-comment">// Start both operations at once</span>
<span class="code-keyword">const</span> [<span class="code-variable">user</span>, <span class="code-variable">posts</span>] = <span class="code-keyword">await</span> Promise.<span class="code-function">all</span>([
<span class="code-function">fetchUser</span>(),
<span class="code-function">fetchPosts</span>()
]);
<span class="code-keyword">return</span> { <span class="code-variable">user</span>, <span class="code-variable">posts</span> }; <span class="code-comment">// Total: 1 second!</span>
}
<span class="code-comment">// Even better: with error handling</span>
<span class="code-keyword">async function</span> <span class="code-function">parallelSafe</span>() {
<span class="code-keyword">const</span> <span class="code-variable">results</span> = <span class="code-keyword">await</span> Promise.<span class="code-function">allSettled</span>([
<span class="code-function">fetchUser</span>(),
<span class="code-function">fetchPosts</span>()
]);
<span class="code-keyword">return</span> <span class="code-variable">results</span>;
}</code></pre>
</div>
</div>
</div>
</section>
<!-- Common Mistakes Section -->
<section class="section" id="mistakes">
<h2 class="section-title">โ Common Mistakes</h2>
<div class="card">
<div class="accordion">
<div class="accordion-item">
<div class="accordion-header" onclick="toggleAccordion(this)">
<span><strong>1. Forgetting the 'async' Keyword</strong></span>
<span class="accordion-icon">โผ</span>
</div>
<div class="accordion-content">
<div class="accordion-body">
<div class="warning">
<strong>Error:</strong> You can't use 'await' in a non-async function!
</div>
<div class="code-block" style="margin-top: 15px;">
<pre><code><span class="code-comment">// โ Wrong - missing 'async'</span>
<span class="code-keyword">function</span> <span class="code-function">fetchData</span>() {
<span class="code-keyword">return await</span> Promise.<span class="code-function">resolve</span>(<span class="code-string">'Hello!'</span>); <span class="code-comment">// SyntaxError!</span>
}
<span class="code-comment">// โ
Correct - has 'async'</span>
<span class="code-keyword">async function</span> <span class="code-function">fetchData</span>() {
<span class="code-keyword">return await</span> Promise.<span class="code-function">resolve</span>(<span class="code-string">'Hello!'</span>); <span class="code-comment">// Works!</span>
}</code></pre>
</div>
</div>
</div>
</div>
<div class="accordion-item">
<div class="accordion-header" onclick="toggleAccordion(this)">
<span><strong>2. Syntax Error: "awaitPromise" (Missing Space!)</strong></span>
<span class="accordion-icon">โผ</span>
</div>
<div class="accordion-content">
<div class="accordion-body">
<div class="warning">
<strong>Common Typo:</strong> Writing "awaitPromise" instead of "await Promise"
</div>
<div class="code-block" style="margin-top: 15px;">
<pre><code><span class="code-comment">// โ Wrong - no space between await and Promise</span>
<span class="code-keyword">async function</span> <span class="code-function">fetchData</span>() {
<span class="code-keyword">return</span> awaitPromise.<span class="code-function">resolve</span>(<span class="code-string">'Hello!'</span>); <span class="code-comment">// ReferenceError!</span>
}
<span class="code-comment">// โ
Correct - space between await and Promise</span>
<span class="code-keyword">async function</span> <span class="code-function">fetchData</span>() {
<span class="code-keyword">return await</span> Promise.<span class="code-function">resolve</span>(<span class="code-string">'Hello!'</span>); <span class="code-comment">// Works!</span>
}</code></pre>
</div>
<p style="margin-top: 15px;"><strong>Remember:</strong> <code>await</code> is a keyword, and <code>Promise</code> is an object. They need a space between them!</p>
</div>
</div>
</div>
<div class="accordion-item">
<div class="accordion-header" onclick="toggleAccordion(this)">
<span><strong>3. Not Handling Errors</strong></span>
<span class="accordion-icon">โผ</span>
</div>
<div class="accordion-content">
<div class="accordion-body">
<div class="warning">
<strong>Problem:</strong> Unhandled promise rejections crash your app!
</div>
<div class="code-block" style="margin-top: 15px;">
<pre><code><span class="code-comment">// โ No error handling</span>
<span class="code-keyword">async function</span> <span class="code-function">fetchData</span>() {
<span class="code-keyword">const</span> <span class="code-variable">data</span> = <span class="code-keyword">await</span> <span class="code-function">fetch</span>(<span class="code-string">'/api/data'</span>); <span class="code-comment">// What if this fails?</span>
<span class="code-keyword">return</span> <span class="code-variable">data</span>;
}
<span class="code-comment">// โ
With error handling</span>
<span class="code-keyword">async function</span> <span class="code-function">fetchData</span>() {
<span class="code-keyword">try</span> {
<span class="code-keyword">const</span> <span class="code-variable">data</span> = <span class="code-keyword">await</span> <span class="code-function">fetch</span>(<span class="code-string">'/api/data'</span>);
<span class="code-keyword">return</span> <span class="code-variable">data</span>;
} <span class="code-keyword">catch</span> (<span class="code-variable">error</span>) {
console.<span class="code-function">error</span>(<span class="code-string">'Failed to fetch:'</span>, <span class="code-variable">error</span>);
<span class="code-keyword">return</span> <span class="code-keyword">null</span>;
}
}</code></pre>
</div>
</div>
</div>
</div>
<div class="accordion-item">
<div class="accordion-header" onclick="toggleAccordion(this)">
<span><strong>4. Unnecessary 'await' on Return</strong></span>
<span class="accordion-icon">โผ</span>
</div>
<div class="accordion-content">
<div class="accordion-body">
<div class="tip">
<strong>Optimization:</strong> You don't always need 'await' when returning!
</div>
<div class="code-block" style="margin-top: 15px;">
<pre><code><span class="code-comment">// Both work, but second is slightly better</span>
<span class="code-keyword">async function</span> <span class="code-function">fetchData1</span>() {
<span class="code-keyword">return await</span> Promise.<span class="code-function">resolve</span>(<span class="code-string">'Hello!'</span>);
}
<span class="code-comment">// Simpler - no need for await here</span>
<span class="code-keyword">async function</span> <span class="code-function">fetchData2</span>() {
<span class="code-keyword">return</span> Promise.<span class="code-function">resolve</span>(<span class="code-string">'Hello!'</span>);
}
<span class="code-comment">// BUT: Use await if you need try/catch</span>
<span class="code-keyword">async function</span> <span class="code-function">fetchData3</span>() {
<span class="code-keyword">try</span> {
<span class="code-keyword">return await</span> Promise.<span class="code-function">resolve</span>(<span class="code-string">'Hello!'</span>);
} <span class="code-keyword">catch</span> (<span class="code-variable">error</span>) {
console.<span class="code-function">error</span>(<span class="code-variable">error</span>);
}
}</code></pre>
</div>
</div>
</div>
</div>
<div class="accordion-item">
<div class="accordion-header" onclick="toggleAccordion(this)">
<span><strong>5. Sequential Instead of Parallel</strong></span>
<span class="accordion-icon">โผ</span>
</div>
<div class="accordion-content">
<div class="accordion-body">
<div class="warning">
<strong>Performance Issue:</strong> Running independent operations sequentially wastes time!
</div>
<div class="code-block" style="margin-top: 15px;">
<pre><code><span class="code-comment">// โ Slow - waits for each one (3 seconds total)</span>
<span class="code-keyword">async function</span> <span class="code-function">slow</span>() {
<span class="code-keyword">const</span> <span class="code-variable">a</span> = <span class="code-keyword">await</span> <span class="code-function">fetchA</span>(); <span class="code-comment">// 1 second</span>
<span class="code-keyword">const</span> <span class="code-variable">b</span> = <span class="code-keyword">await</span> <span class="code-function">fetchB</span>(); <span class="code-comment">// 1 second</span>
<span class="code-keyword">const</span> <span class="code-variable">c</span> = <span class="code-keyword">await</span> <span class="code-function">fetchC</span>(); <span class="code-comment">// 1 second</span>
<span class="code-keyword">return</span> [<span class="code-variable">a</span>, <span class="code-variable">b</span>, <span class="code-variable">c</span>];
}
<span class="code-comment">// โ
Fast - runs in parallel (1 second total)</span>
<span class="code-keyword">async function</span> <span class="code-function">fast</span>() {
<span class="code-keyword">const</span> [<span class="code-variable">a</span>, <span class="code-variable">b</span>, <span class="code-variable">c</span>] = <span class="code-keyword">await</span> Promise.<span class="code-function">all</span>([
<span class="code-function">fetchA</span>(),
<span class="code-function">fetchB</span>(),
<span class="code-function">fetchC</span>()
]);
<span class="code-keyword">return</span> [<span class="code-variable">a</span>, <span class="code-variable">b</span>, <span class="code-variable">c</span>];
}</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<!-- Pro Tips Section -->
<section class="section" id="pro-tips">
<h2 class="section-title">๐ Pro Tips</h2>
<div class="card">
<div class="cheat-grid">
<div class="cheat-item">
<h4>โก Promise.all()</h4>
<p>Run multiple async operations in parallel for better performance!</p>
</div>
<div class="cheat-item">
<h4>๐ก๏ธ Promise.allSettled()</h4>
<p>Like Promise.all() but doesn't fail if one promise rejects!</p>
</div>
<div class="cheat-item">
<h4>๐ Promise.race()</h4>
<p>Returns the first promise that resolves or rejects!</p>
</div>
<div class="cheat-item">
<h4>๐ Top-Level Await</h4>
<p>In ES modules, you can use await at the top level!</p>
</div>
<div class="cheat-item">
<h4>๐ฏ Arrow Functions</h4>
<p><code>const func = async () => {...}</code> works great!</p>
</div>
<div class="cheat-item">
<h4>๐ Debugging</h4>
<p>Use <code>console.log()</code> before await to track execution!</p>
</div>
</div>
<div class="success" style="margin-top: 30px;">
<strong>๐ Best Practice:</strong> Always use try/catch for error handling in async functions, especially when dealing with external APIs or file operations!
</div>
</div>
</section>
<!-- Quiz Section -->
<section class="section" id="quiz">
<h2 class="section-title">๐ฏ Knowledge Check Quiz</h2>
<div class="quiz-container">
<p style="margin-bottom: 30px;">Test your understanding! Click on the correct answer. ๐ง </p>
<div class="quiz-question">
<h3>Question 1: What does the 'async' keyword do?</h3>
<div class="quiz-options">
<div class="quiz-option" onclick="checkAnswer(this, false, 1)">
A) Makes the function run faster
</div>
<div class="quiz-option" onclick="checkAnswer(this, true, 1)">
B) Makes the function return a Promise
</div>
<div class="quiz-option" onclick="checkAnswer(this, false, 1)">
C) Pauses the function execution
</div>
<div class="quiz-option" onclick="checkAnswer(this, false, 1)">
D) Handles errors automatically
</div>
</div>
<div class="quiz-feedback" id="feedback1"></div>
</div>
<div class="quiz-question">
<h3>Question 2: What's wrong with "awaitPromise.resolve()"?</h3>
<div class="quiz-options">
<div class="quiz-option" onclick="checkAnswer(this, false, 2)">
A) Nothing, it's correct
</div>
<div class="quiz-option" onclick="checkAnswer(this, true, 2)">
B) Missing space between 'await' and 'Promise'
</div>
<div class="quiz-option" onclick="checkAnswer(this, false, 2)">
C) Should use 'async' instead
</div>
<div class="quiz-option" onclick="checkAnswer(this, false, 2)">
D) Promise.resolve() doesn't exist
</div>
</div>
<div class="quiz-feedback" id="feedback2"></div>
</div>
<div class="quiz-question">
<h3>Question 3: How do you run multiple async operations in parallel?</h3>
<div class="quiz-options">
<div class="quiz-option" onclick="checkAnswer(this, false, 3)">
A) Use multiple await statements
</div>
<div class="quiz-option" onclick="checkAnswer(this, true, 3)">
B) Use Promise.all() with await
</div>
<div class="quiz-option" onclick="checkAnswer(this, false, 3)">
C) Use async twice
</div>
<div class="quiz-option" onclick="checkAnswer(this, false, 3)">
D) You can't run them in parallel
</div>
</div>
<div class="quiz-feedback" id="feedback3"></div>
</div>
<div id="quizScore" style="margin-top: 30px; padding: 20px; background: var(--bg-secondary); border-radius: 12px; display: none;">
<h3 style="color: var(--accent-green);">๐ Quiz Complete!</h3>
<p id="scoreText" style="font-size: 1.2rem; margin-top: 10px;"></p>
</div>
</div>
</section>
<!-- Cheat Sheet Section -->
<section class="section" id="cheat-sheet">
<h2 class="section-title">๐ Quick Reference Cheat Sheet</h2>
<div class="cheat-sheet">
<h3 style="margin-bottom: 20px; color: #1a202c;">Save this for later! ๐</h3>
<div class="cheat-grid">
<div class="cheat-item">
<h4>๐ async Keyword</h4>
<p><code>async function name() {}</code></p>
<p style="font-size: 0.85rem; margin-top: 5px;">Makes function return Promise</p>
</div>
<div class="cheat-item">
<h4>โธ๏ธ await Keyword</h4>
<p><code>await promise</code></p>
<p style="font-size: 0.85rem; margin-top: 5px;">Pauses until Promise resolves</p>
</div>
<div class="cheat-item">
<h4>๐ก๏ธ Error Handling</h4>
<p><code>try { await... } catch(e) {}</code></p>
<p style="font-size: 0.85rem; margin-top: 5px;">Handle async errors</p>
</div>
<div class="cheat-item">
<h4>โก Parallel</h4>
<p><code>await Promise.all([...])</code></p>
<p style="font-size: 0.85rem; margin-top: 5px;">Run multiple at once</p>
</div>
<div class="cheat-item">
<h4>๐ Race</h4>
<p><code>await Promise.race([...])</code></p>
<p style="font-size: 0.85rem; margin-top: 5px;">First to finish wins</p>
</div>
<div class="cheat-item">
<h4>โ
All Settled</h4>
<p><code>await Promise.allSettled([...])</code></p>
<p style="font-size: 0.85rem; margin-top: 5px;">Wait for all, no fail</p>
</div>
</div>
<div style="margin-top: 30px; padding: 20px; background: rgba(255,255,255,0.2); border-radius: 12px;">
<h4 style="color: #1a202c; margin-bottom: 15px;">๐ฏ Complete Template</h4>
<div class="code-block">
<pre><code><span class="code-comment">// Basic async/await pattern</span>
<span class="code-keyword">async function</span> <span class="code-function">fetchData</span>() {
<span class="code-keyword">try</span> {
<span class="code-keyword">const</span> <span class="code-variable">response</span> = <span class="code-keyword">await</span> <span class="code-function">fetch</span>(<span class="code-string">'/api/data'</span>);
<span class="code-keyword">const</span> <span class="code-variable">data</span> = <span class="code-keyword">await</span> <span class="code-variable">response</span>.<span class="code
Live Preview