SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
JavaScript
1. JavaScript Tutorial for Beginners — Learn JS from Scratch 2. Where to Put JavaScript Code — Head, Body or .js File 3. JavaScript Output Methods — alert, innerHTML, console.log 4. JavaScript Syntax Explained — Statements & Rules 5. JavaScript Comments — How to Write // and /* */ 6. JavaScript Variables — var, let, const Explained 7. JavaScript Operators — Arithmetic, Comparison, Logical 8. JavaScript Data Types — string, number, object, array 9. JavaScript Functions Tutorial — Parameters & Return Values 10. JavaScript Objects — Properties, Methods & this Keyword 11. JavaScript Scope — Global, Local & Block Scope Explained 12. JavaScript Events — addEventListener with Examples 13. JavaScript Strings — Methods, slice, replace & Templates 14. JavaScript Numbers — parseInt, toFixed, NaN Explained 15. JavaScript Arrays — Create, Read, push, pop & More 16. JavaScript Array Methods — map, filter, forEach, find 17. JavaScript Date Object — new Date(), getFullYear & More 18. JavaScript Math Object — random, round, floor, ceil 19. JavaScript if else — Conditions with Examples 20. JavaScript switch Statement — case, break, default 21. JavaScript for Loop — for, for...of, for...in Examples 22. JavaScript while Loop — while & do...while Explained 23. JavaScript Type Conversion — Number, String, Boolean 24. JavaScript try catch — Error Handling with finally 25. JavaScript JSON — stringify & parse with Examples 26. JavaScript Arrow Functions — => Syntax & this 27. JavaScript Classes — Constructor & Objects Explained 28. JavaScript Promises & async/await Explained Simply 29. JavaScript DOM Tutorial — Find & Change Page Elements 30. JavaScript Form Validation Tutorial with Examples 31. JavaScript Fetch API Tutorial — fetch() with Examples 32. JavaScript localStorage — setItem, getItem Explained 33. JavaScript Timers — setTimeout & setInterval Examples 34. JavaScript Regular Expressions — test() & match() Guide 35. JavaScript Debugging — console.log & Fixing Errors
Final Test — Certificate ₹10
Try this code live
Lesson 28 of 35

JavaScript Promises & async/await Explained Simply

4 min read · Free · No login needed

Most JavaScript runs instantly: add two numbers, show a message, done. But some work takes time — asking a server for data, waiting for a file to load, or simply waiting two seconds. JavaScript does not freeze the page while it waits. Instead it makes a Promise: "I will give you an answer later." Your code can carry on with other things, and when the answer arrives, JavaScript lets you know.

Diagram — see how it fits together
Diagram: JavaScript Promises and async/await

Think of ordering chai at a busy shop. You do not stand at the counter blocking everyone else — you get a token number, sit down, and the shop calls your number when the chai is ready. The token is the Promise. The call is the answer. And async/await is like sitting at the table and simply waiting for your number to be called, without doing anything else in between.

Promises — then and catch

let order = new Promise((resolve, reject) => {
  setTimeout(() => resolve("Your chai is ready!"), 2000);
});
order.then(message => console.log(message))
     .catch(error => console.log("Failed: " + error));

A Promise has three states: pending (waiting), fulfilled (done, with a value) and rejected (failed, with a reason). You create one with new Promise and two functions: resolve for success and reject for failure. .then() runs when the promise succeeds and receives the value. .catch() runs when it fails and receives the reason. You can chain them: each .then() passes its result to the next one, like a line of people passing a parcel.

Note: A promise that is rejected with no .catch() anywhere becomes an "unhandled rejection" — the browser complains in the console, and the failure is easy to miss. Always end a promise chain with a .catch(), even if it only logs the error.

async/await — waiting in plain words

async function orderChai() {
  try {
    let message = await order;
    console.log(message);
  } catch (error) {
    console.log("Failed: " + error);
  }
}

async/await is the same idea written in plain words. Mark a function async, and inside it you can write await before a promise — the function pauses there until the promise settles, then carries on with the value, as if the waiting never happened. Errors are caught with the same try...catch you already know. It reads like normal step-by-step code, which is why it is the standard style in real projects.

Two rules: await only works inside an async function, and an async function always returns a promise itself — even if you return a plain value, it comes back wrapped in a promise.

setTimeout — the simplest wait

setTimeout(function, milliseconds) runs a function once, after a delay. setTimeout(() => console.log("Done"), 2000) waits two seconds, then prints. It is not a promise itself, but you can wrap it in one — which is exactly what the example below does. setInterval is its cousin: it repeats the function every so many milliseconds, until you stop it with clearInterval.

Tip: Remember it like this: promise now, answer later — then, catch, or await. Use .then() for short chains and async/await when the steps read better top to bottom. Either way, always catch the failure.

Common mistakes

  • Forgetting .catch() — a rejected promise with no handler fails silently in your code and loudly in the console.
  • Using await outside an async function — it is a syntax error. The function must be marked async.
  • Thinking await freezes the whole page — it only pauses that one function. The rest of the page keeps working.
  • Returning a value from an async function and treating it as a plain value — it is a promise, so you need .then() or another await to read it.

Remember

  • A Promise is pending, then fulfilled or rejected. resolve succeeds, reject fails.
  • .then() handles success, .catch() handles failure. Chain them in order.
  • async/await reads like step-by-step code; await only works inside async.
  • setTimeout(fn, ms) runs a function once after a delay.
  • Promise now, answer later — then, catch, or await.

Example explained

  • function makeChai() { return new Promise((resolve) => { ... }); } — a function that returns a promise. The chai takes 2 seconds, so the promise settles later.
  • setTimeout(() => resolve("Your masala chai is ready!"), 2000); — after 2000 milliseconds the promise is fulfilled with the message.
  • document.getElementById("out").textContent = "Making your chai..."; — the status changes immediately, so the user knows the order was accepted.
  • makeChai().then(message => { ... }); — when the promise settles, the message is shown on the page. Nothing is frozen while waiting.
  • .catch(error => { ... }) — if anything failed, the error message would be shown instead. Always catch.

Example

Example
<!DOCTYPE html>
<html>
<head>
<title>Order Chai — Promises</title>
<style>
body { font-family: Verdana, sans-serif; background-color: #fff8e1; }
h1 { color: #b45309; text-align: center; }
.card { background-color: white; border: 2px solid #f59e0b; padding: 16px; width: 340px; margin: 20px auto; }
button { background-color: #b45309; color: white; border: none; padding: 10px 18px; font-size: 16px; }
#out { font-weight: bold; color: green; }
</style>
</head>
<body>

<h1>Order Chai</h1>
<div class="card">
<p><button onclick="orderChai()">Order masala chai</button></p>
<p id="out">Your chai status will appear here.</p>
<p>Try it: click the button — the status changes at once, and the chai is ready after 2 seconds.</p>
</div>

<script>
function makeChai() {
  return new Promise((resolve) => {
    setTimeout(() => resolve("Your masala chai is ready!"), 2000);
  });
}
function orderChai() {
  document.getElementById("out").textContent = "Making your chai...";
  makeChai()
    .then(message => {
      document.getElementById("out").textContent = message;
    })
    .catch(error => {
      document.getElementById("out").textContent = "Failed: " + error;
    });
}
</script>

</body>
</html>

Output

Output — how it looks in the browser
Output of JavaScript Promises & async/await Explained Simply

Try it yourself — live editor

Change the code below and press Run to see the result instantly. No file to save, no login needed.

Result

Frequently asked questions

The questions learners ask most about this lesson — read the question, then open it to see the answer.

1. What are the three states of a Promise?

Pending (waiting), fulfilled (done, with a value) and rejected (failed, with a reason).

2. What happens if a rejected promise has no .catch()?

It becomes an unhandled rejection: the browser complains in the console and the failure is easy to miss.

3. Can I use await outside an async function?

No, it is a syntax error — the function must be marked async, and an async function always returns a promise itself.

Why learn this yourself — and not just ask AI?

AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.

Get certified — just ₹10

Finish all 35 lessons, pass the test (80%), pay ₹10 and get your certificate.

Take the test

Prefer watching? Video courses teach the same topics step by step.

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

Master Python + AI Skills in 30 Days
₹499
View
Full Stack PHP Development Course 2026
₹3,000
View
Summer Training in Python Programming 2026
₹3,000
View
Browse all courses
JavaScript — Lessons
1. JavaScript Tutorial for Beginners — Learn JS from Scratch 2. Where to Put JavaScript Code — Head, Body or .js File 3. JavaScript Output Methods — alert, innerHTML, console.log 4. JavaScript Syntax Explained — Statements & Rules 5. JavaScript Comments — How to Write // and /* */ 6. JavaScript Variables — var, let, const Explained 7. JavaScript Operators — Arithmetic, Comparison, Logical 8. JavaScript Data Types — string, number, object, array 9. JavaScript Functions Tutorial — Parameters & Return Values 10. JavaScript Objects — Properties, Methods & this Keyword 11. JavaScript Scope — Global, Local & Block Scope Explained 12. JavaScript Events — addEventListener with Examples 13. JavaScript Strings — Methods, slice, replace & Templates 14. JavaScript Numbers — parseInt, toFixed, NaN Explained 15. JavaScript Arrays — Create, Read, push, pop & More 16. JavaScript Array Methods — map, filter, forEach, find 17. JavaScript Date Object — new Date(), getFullYear & More 18. JavaScript Math Object — random, round, floor, ceil 19. JavaScript if else — Conditions with Examples 20. JavaScript switch Statement — case, break, default 21. JavaScript for Loop — for, for...of, for...in Examples 22. JavaScript while Loop — while & do...while Explained 23. JavaScript Type Conversion — Number, String, Boolean 24. JavaScript try catch — Error Handling with finally 25. JavaScript JSON — stringify & parse with Examples 26. JavaScript Arrow Functions — => Syntax & this 27. JavaScript Classes — Constructor & Objects Explained 28. JavaScript Promises & async/await Explained Simply 29. JavaScript DOM Tutorial — Find & Change Page Elements 30. JavaScript Form Validation Tutorial with Examples 31. JavaScript Fetch API Tutorial — fetch() with Examples 32. JavaScript localStorage — setItem, getItem Explained 33. JavaScript Timers — setTimeout & setInterval Examples 34. JavaScript Regular Expressions — test() & match() Guide 35. JavaScript Debugging — console.log & Fixing Errors
Final Test — Certificate ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Please log in to download. You will stay on this page after logging in!

New here? Register now →