Day 11/100 of JavaScript 🚀 Today’s topic: Promises A Promise represents the result of an asynchronous operation It has 3 states: - Pending - Fulfilled - Rejected 🔹Creating a Promise const promise = new Promise((resolve, reject) => { let success = true; if (success) { resolve("Done"); } else { reject("Error"); } }); 🔹Consuming a Promise promise .then(result => console.log(result)) .catch(error => console.error(error)); 🔹Chaining fetch(url) .then(res => res.json()) .then(data => console.log(data)) .catch(err => console.error(err)); Promises help handle asynchronous operations in a structured way and avoid deeply nested callbacks. #Day11 #JavaScript #100DaysOfCode
Understanding JavaScript Promises
More Relevant Posts
-
💡 𝗧𝗶𝗽 𝗼𝗳 𝘁𝗵𝗲 𝗗𝗮𝘆 — 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗗𝗶𝗱 𝘆𝗼𝘂 𝗸𝗻𝗼𝘄? "setTimeout(fn, 0)" doesn’t run immediately — it’s queued in the 𝗲𝘃𝗲𝗻𝘁 𝗹𝗼𝗼𝗽. Even with a delay of "0", the callback only runs 𝗮𝗳𝘁𝗲𝗿 𝘁𝗵𝗲 𝗰𝘂𝗿𝗿𝗲𝗻𝘁 𝗰𝗮𝗹𝗹 𝘀𝘁𝗮𝗰𝗸 𝗶𝘀 𝗰𝗹𝗲𝗮𝗿𝗲𝗱. 🔧 𝗪𝗵𝘆 𝘁𝗵𝗶𝘀 𝗺𝗮𝘁𝘁𝗲𝗿𝘀: - Helps you understand async behavior - Useful for deferring execution without blocking - Explains why some code runs later than expected Understanding the event loop = mastering JavaScript timing. #JavaScript #AsyncJavaScript #FrontendDevelopment #WebDevelopment #SoftwareEngineering #CodingTips #EventLoop #FullstackDeveloper
To view or add a comment, sign in
-
-
🚀 Day 2/30 – JavaScript Challenge LeetCode Problem: 2620 – Counter Today I learned about one of the most important concepts in JavaScript Closures. 🔹 Concept Explained: The inner function remembers the variable number even after the outer function has finished execution. This is called a closure. 🔹 Key Learnings: ✅ Closures help maintain state without global variables ✅ Useful in counters, ID generators, and real-world applications ✅ number++ returns the current value, then increments it #Leetcode #Day2 #JavaScript #Developers #Frontend
To view or add a comment, sign in
-
-
Day 4 of 30 Days of TypeScript 🚀 Today’s topic: any vs unknown A lot of developers use any thinking it gives flexibility… But in reality, it removes the biggest advantage of TypeScript — type safety. Here’s the truth: 🔴 any Disables type checking Allows anything (even wrong code) Can lead to runtime bugs 🟢 unknown Forces you to validate data Keeps your code safe Encourages better patterns Example 👇 let data: unknown = "hello"; if (typeof data === "string") { console.log(data.toUpperCase()); // ✅ safe } 👉 Rule of thumb: If you don’t know the type yet, use unknown — not any. Because good developers don’t just make code work… They make it reliable. #TypeScript #WebDevelopment #JavaScript #Frontend #CleanCode #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 955 of #1000DaysOfCode ✨ How JavaScript Event Loop Works Behind the Curtains JavaScript looks simple on the surface — but under the hood, a lot is happening to make async code work smoothly. In today’s post, I’ve explained how the JavaScript Event Loop actually works behind the scenes, so you can understand how tasks are executed, queued, and prioritized. From the call stack to the callback queue and microtask queue, this concept explains why some functions run before others — even when the code looks sequential. Understanding the event loop helps you debug tricky async issues, avoid unexpected behavior, and write more predictable code. If you’re working with promises, async/await, or APIs, this is one of those concepts you must truly understand. 👇 What part of the event loop confuses you the most — call stack, microtasks, or callbacks? #Day955 #learningoftheday #1000daysofcodingchallenge #FrontendDevelopment #WebDevelopment #JavaScript #React #CodingCommunity #AsyncJavaScript
To view or add a comment, sign in
-
🚀 #JavaScript Event Loop If you want to truly understand JavaScript async behavior, you must understand the Event Loop. 👉 What is Event Loop? It’s a mechanism that allows JavaScript to handle asynchronous operations using: • Call Stack • Web APIs • Task Queues (Microtask Queue & Macrotask Queue) 👉 Execution Flow: 1️⃣ Synchronous code runs first (Call Stack) 2️⃣ Then Microtasks execute → Promises, queueMicrotask 3️⃣ Then Macrotasks execute → setTimeout, setInterval ⚡ Rule: Microtasks always run before macrotasks. 💻 Example: console.log('Hi'); Promise.resolve().then(() => { console.log('Promise'); }); setTimeout(() => { console.log('Timeout'); }, 0); console.log('End'); ✅ Output: Hi End Promise Timeout 🧠 Why? Sync code runs first → Hi, End Promise goes to Microtask Queue → runs next setTimeout goes to Macrotask Queue → runs last #javascript #eventloop #promises #asyncjavascript #frontenddeveloper #webdevelopment #coding #100daysofcode #learnjavascript #developerlife #programming #jsdeveloper #tech #softwaredeveloper
To view or add a comment, sign in
-
-
🚀 𝐃𝐚𝐲 𝟏𝟐/𝟏𝟓 𝐨𝐟 𝐌𝐲 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠 𝐒𝐞𝐫𝐢𝐞𝐬 Today I learned about Callback Functions in JavaScript 💡 👉 A callback is a function that is passed as an argument to another function. 📌 Example: function greet(name, callback) { console.log("Hello " + name); callback(); } function sayBye() { console.log("Goodbye!"); } greet("Kanishka", sayBye); 👉 Output: Hello Kanishka Goodbye! 👉 Why use callbacks? To control execution order To run code after something finishes 👉 Callbacks are widely used in async operations 💻✨ 💬 Question: Do you find callbacks easy or confusing? Let’s learn together 🚀 #JavaScript #WebDevelopment #LearningInPublic #Day12 #FrontendDevelopment
To view or add a comment, sign in
-
-
🚀 Day 1/30 – JavaScript Challenge LeetCode 2667 – Create Hello World Function 🧩 Problem: Write a function that returns a new function. That new function should always return "Hello World", no matter what arguments are passed. 🧠 Explanation: createHelloWorld() returns another function. The returned function uses (...args) to accept any number of arguments. But we ignore all inputs and always return "Hello World". 💡 Key Concept: This problem is based on: Higher Order Functions (function returning function). Rest Parameters (...args). Function independence from input. #javascript #30Days #Leetcode
To view or add a comment, sign in
-
-
Day 5/100 of JavaScript Today’s Topic: Closures in JavaScript A closure is created when a function remembers variables from its outer scope even after that outer function has finished execution Example: function outer() { let count = 0; return function inner() { count++; return count; }; } const counter = outer(); counter(); // 1 counter(); // 2 Here, the "inner" function forms a closure over the "count" variable. Even though "outer()" has finished execution, "count" is preserved. Key understanding: Closures help in maintaining state and also enable data privacy by restricting direct access to variables #Day5 #JavaScript #100DaysOfCode
To view or add a comment, sign in
-
Day 7 of My JavaScript Journey 🚀 Today, I learned about the switch statement and the difference between statements and expressions in JavaScript. The switch statement is used as an alternative to multiple if/else conditions, especially when dealing with specific values. Example: switch(day) { case "Monday": console.log("Start of the week"); break; default: console.log("Another day"); } I also learned the difference between statements and expressions: • An expression produces a value Example: 5 + 3 • A statement performs an action Example: if (condition) { ... } One key insight: Understanding this difference helps in writing cleaner and more predictable code. Key takeaway: switch is used for cleaner condition handling and understand when you're producing a value vs performing an action. #JavaScript #WebDevelopment #LearningInPublic #100DaysOfCode
To view or add a comment, sign in
-
🧠 Day 3 of 21 days challenge JavaScript Event Loop 🤯 Event Loop is a mechanism in JavaScript that handles execution of asynchronous code. It continuously checks the call stack and callback queue. If the stack is empty, it moves tasks from the queue to the stack for execution. For example :- console.log("Start"); console.log("End"); console.log("Timeout"); Wait… why this order? Because JavaScript doesn’t run everything instantly. It uses: • Call Stack • Web APIs • Callback Queue Event Loop decides what runs next. 💤For easy understanding :- Event Loop = decides execution order Sync code runs first Async code waits in queue Then runs after the stack is empty 👉 That’s why “Timeout” runs last This changed how I understand async code 🚀 #JavaScript #EventLoop #Async
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development