Async JavaScript — what actually matters beyond the basics. 🚀 📌 Promises have 3 states — and they're irreversible. Once settled, a Promise can never go back to pending. That's by design. 📌 .catch covers the entire chain above it. Any error thrown anywhere flows down automatically. Not just the last step. The whole chain. 📌 fetch doesn't throw on 404 or 500. It only throws on network failure. Always check res.ok explicitly — try/catch alone won't save you here. 📌 Four combinators. Four different jobs. ✅ Promise.all → all required, fail fast ✅ Promise.allSettled → independent optionals, handle each separately ✅ Promise.race → timeout enforcement ✅ Promise.any → multiple sources, first success wins 📌 Sequential awaits are a hidden performance trap. If two calls don't depend on each other — they should run in parallel. Every unnecessary sequential await is wasted time. 📌 Retrying immediately under failure makes things worse. All instances retrying at the same moment hammer a struggling API further. Exponential backoff + jitter staggers retries — giving the API room to recover. Small decisions in async code have large consequences at scale. 💡 #JavaScript #AsyncJS #WebPerformance #FrontendEngineering #PlatformEngineering
Async JavaScript: Beyond the Basics
More Relevant Posts
-
⚡ Most developers accidentally make async JavaScript slower than it needs to be. A lot of people write async code like this: await first request wait… await second request wait… await third request It works. But if those requests are independent, you’re wasting time. The better approach: ✅ run them in parallel with Promise.all() That small change can make your code feel much faster without changing the feature at all. Simple rule: If task B depends on task A → use sequential await If tasks are independent → use Promise.all() This is one of those JavaScript habits that instantly makes you look more senior 👀 Join 3,000+ developers on my Substack: 👉 https://lnkd.in/dTdunXEJ How often do you see this mistake in real codebases? #JavaScript #WebDevelopment #Frontend #SoftwareEngineering #AsyncJavaScript #Promises #CodingTips #Developers #LearnToCode #AITechDaily
To view or add a comment, sign in
-
-
#One_real_production_lesson_frontend_dev_taught_me: We had a bug in production where an API was getting called twice. At first, it looked like a backend issue. But the backend logs were clean. So I started digging into the frontend 👇 The culprit? 👉 React 18 Strict Mode. In development, React intentionally runs components twice to detect side effects. But our code wasn’t written with that in mind. Here’s what we had: ❌ API call inside "useEffect" without proper safeguards ❌ No cleanup / idempotent logic ❌ Assumption: "useEffect runs only once" Result: Duplicate API calls → inconsistent data → confused users. --- ✅ Fix: - Made API calls idempotent - Added proper checks before firing requests - Avoided unnecessary side effects inside "useEffect" --- 💡 Lesson: Writing code that works is easy. Writing code that works in real-world scenarios is the real skill. React doesn’t cause bugs. Our assumptions do. --- Since then, I always ask: 👉 “What happens if this runs twice?” 👉 “Is this safe in concurrent rendering?” --- Still learning something new every day 🚀 #FrontendDevelopment #ReactJS #JavaScript #CleanCode #WebDevelopment #Debugging
To view or add a comment, sign in
-
I reviewed 50+ full-stack projects last quarter. Only about 20% truly nailed end-to-end type safety. tRPC has been a game changer for me. It eliminates the usual friction between backend and frontend types by sharing them seamlessly. You write your API once, and TypeScript guarantees consistency everywhere—no more manual syncing or guesswork. Here’s a quick tRPC router example I built using vibe coding to rapidly prototype a user API: ```ts import { initTRPC } from '@trpc/server'; const t = initTRPC.create(); const appRouter = t.router({ getUser: t.procedure .input((id: string) => id) .query(({ input }) => { return { id: input, name: 'Alice' }; }), }); type AppRouter = typeof appRouter; ``` With this setup, my React frontend can consume `getUser` with full type safety—from input validation to response shape—without writing extra types or API clients. The productivity boost is real. Have you tried integrating tRPC or similar end-to-end type-safe tools in your stack? What challenges did you face? #WebDevelopment #TypeScript #Frontend #JavaScript
To view or add a comment, sign in
-
Async/Await vs Promises — When to Use What? Both handle async operations, but here's my rule: Use Promises when: You need to run multiple operations in parallel Using .all() or .race() Use Async/Await when: You need sequential execution Readability matters (and it always does!) Error handling with try/catch is cleaner Example: // Clean sequential flow async function getData() { try { const user = await getUser(); const posts = await getPosts(user.id); return posts; } catch (error) { console.error(error); } } What's your preference? Async/await or Promises? #JavaScript #NodeJS #AsyncProgramming #CodingTips #WebDev
To view or add a comment, sign in
-
🚨 𝗪𝗵𝘆 𝗬𝗼𝘂𝗿 𝗥𝗲𝗮𝗰𝘁 𝘂𝘀𝗲𝗘𝗳𝗳𝗲𝗰𝘁 𝗥𝘂𝗻𝘀 𝗠𝘂𝗹𝘁𝗶𝗽𝗹𝗲 𝗧𝗶𝗺𝗲𝘀 (𝗔𝗻𝗱 𝗛𝗼𝘄 𝘁𝗼 𝗙𝗶𝘅 𝗜𝘁 𝗪𝗶𝘁𝗵𝗼𝘂𝘁 𝗕𝗿𝗲𝗮𝗸𝗶𝗻𝗴 𝗟𝗼𝗴𝗶𝗰) You write a simple `𝘂𝘀𝗲𝗘𝗳𝗳𝗲𝗰𝘁`… But your API gets called twice. 🤯 You check everything—dependencies, logic, structure. Still happens. 👉 If you're using 𝗥𝗲𝗮𝗰𝘁 𝟭𝟴+, this is likely due to 𝗦𝘁𝗿𝗶𝗰𝘁 𝗠𝗼𝗱𝗲 in development. React intentionally runs effects twice to detect side effects and ensure your logic is reliable. 💡 𝗜𝗺𝗽𝗼𝗿𝘁𝗮𝗻𝘁: This doesn’t happen in production. It’s just a dev safety check. ✅ 𝗪𝗵𝗮𝘁 𝘆𝗼𝘂 𝘀𝗵𝗼𝘂𝗹𝗱 𝗱𝗼 𝗶𝗻𝘀𝘁𝗲𝗮𝗱: • Write idempotent effects (safe to run multiple times) • Add proper cleanup (abort fetch, remove listeners) • Avoid relying on single execution assumptions ⚠️ Don’t disable Strict Mode blindly—it’s helping you catch hidden bugs early. 💬 Have you faced this issue before? How did you handle it? #ReactJS #FrontendDevelopment #WebDevelopment #JavaScript #ReactTips #SoftwareEngineering
To view or add a comment, sign in
-
🚀 𝗨𝗻𝗱𝗲𝗿𝘀𝘁𝗮𝗻𝗱𝗶𝗻𝗴 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗖𝗹𝗼𝘀𝘂𝗿𝗲𝘀 (The Secret Locker Story 😂) Ever felt like JavaScript remembers things even after they’re gone? 👀 Well… it actually does 😎 Let me explain 𝗖𝗹𝗼𝘀𝘂𝗿𝗲𝘀 in the simplest (and funniest) way 👇 🔐 1. Create a Locker with a Secret /* JavaScript * / function createLocker() { let secret = "💰 1 Crore Password"; return function() { console.log(secret); }; } JS be like: “Okay… I’ll keep this secret safe 🤫” 🎁 𝗧𝗮𝗸𝗲 𝘁𝗵𝗲 𝗟𝗼𝗰𝗸𝗲𝗿 𝗞𝗲𝘆 /* JavaScript * / const myLocker = createLocker(); Now the outer function is gone… finished… bye bye 👋 😳 𝗦𝗲𝗰𝗿𝗲𝘁 𝗔𝗯𝗵𝗶 𝗕𝗵𝗶 𝗟𝗶𝘃𝗲 𝗛𝗮𝗶 /* JavaScript * / myLocker(); // 💰 1 Crore Password JS says: “Function gaya toh kya hua… memory toh mere paas hai 😎” 🧠 𝗪𝗵𝗮𝘁 𝗷𝘂𝘀𝘁 𝗵𝗮𝗽𝗽𝗲𝗻𝗲𝗱? 👉 Inner function remembers outer function’s variables 👉 Even after outer function is finished 👉 This is called a Closure 😂 𝗥𝗲𝗮𝗹-𝗟𝗶𝗳𝗲 𝗔𝗻𝗮𝗹𝗼𝗴𝘆 It’s like: 👩 Mom hides sweets in a locker 🍫 🔑 Gives you the key 🏠 Leaves the house And you’re like: “अब तो मज़े ही मज़े 😎” ⚠️ 𝗜𝗺𝗽𝗼𝗿𝘁𝗮𝗻𝘁 𝗣𝗼𝗶𝗻𝘁 /* JavaScript * / const locker1 = createLocker(); const locker2 = createLocker(); 👉 Both lockers have their own secret (No sharing bro ❌😆) 💼 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗢𝗻𝗲-𝗟𝗶𝗻𝗲𝗿 Closure = A function that remembers variables from its outer scope even after the outer function has executed. 🔥 𝗡𝗼𝘄 𝘆𝗼𝘂𝗿 𝘁𝘂𝗿𝗻! Can you think of real use cases of closures? Drop in comments 👇👇 #JavaScript #WebDevelopment #Frontend #Coding #Programming #Developers #LearnToCode #Tech #SoftwareEngineering #ReactJS #100DaysOfCode #CodingLife
To view or add a comment, sign in
-
Day 3 ⚡ JavaScript Promises — Quick Revision If you're learning async JavaScript, understanding Promises is a must. --- 🧠 What is a Promise? 👉 A Promise represents a value that will be available now, later, or never --- 🔄 Promise States Pending ⏳ Fulfilled ✅ Rejected ❌ --- ✅ Basic Example const promise = new Promise((resolve, reject) => { resolve("Success"); }); --- 🎯 Using Promises promise .then(res => console.log(res)) .catch(err => console.log(err)); --- 🔗 Chaining (Very Important) Promise.resolve(2) .then(res => res * 2) .then(res => res + 5) .then(res => console.log(res)); // 9 👉 Each .then() must return a value --- ⚠️ Common Mistakes ❌ Not returning inside .then() ❌ Breaking the chain ❌ Delaying .then() instead of resolve() --- 💡 One-line takeaway: 👉 Promises help you control async code step-by-step --- Master this, and async JavaScript becomes much easier 🚀 #JavaScript #WebDevelopment #Frontend #Coding #100DaysOfCode
To view or add a comment, sign in
-
🚨 JavaScript Hoisting – Something Most Developers Still Misunderstand Most developers say: 👉 “JavaScript moves variables to the top of the scope.” But that’s not actually what happens. Let’s test this 👇 console.log(a); var a = 10; Output: undefined Now try this: console.log(b); let b = 20; Output: ReferenceError: Cannot access 'b' before initialization 💡 Why the difference? Both var and let are hoisted. But the real difference is initialization timing. ✔ var is hoisted and initialized with undefined during the creation phase. ✔ let and const are hoisted but stay inside the Temporal Dead Zone (TDZ) until the line where they are declared. That’s why accessing them before declaration throws an error. 👉 So technically: JavaScript doesn’t “move variables to the top”. Instead, the JavaScript engine allocates memory for declarations during the creation phase of the execution context. Small detail. But it explains a lot of confusing bugs. 🔥 Understanding this deeply helps when debugging closures, scope issues, and async code. #javascript #frontend #webdevelopment #reactjs #coding #softwareengineering
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