Day 16 of #30DaysOfJavaScript on LeetCode Today's Challenge: 2637 — Promise Time Limit Today’s challenge focused on controlling asynchronous execution using timeouts. The goal was to wrap any async function with a time limit — if the function doesn't finish in time, it should automatically reject with "Time Limit Exceeded". Here’s my solution: var timeLimit = function(fn, t) { return async function(...args) { const timeOutPromise = new Promise((_, reject) => setTimeout(() => reject("Time Limit Exceeded"), t) ) return Promise.race([fn(...args), timeOutPromise]) } } Try it out here: https://lnkd.in/g6WC5mu7 This exercise helped me understand how to combine Promise.race() with timeouts to enforce execution limits #JavaScript #LeetCode #CodingChallenge #AsyncAwait #Promises #30DaysOfCode #Programming #Developers #LearningJourney
Controlling async execution with time limits in JavaScript
More Relevant Posts
-
Day 10 of #30DaysOfJavaScript on LeetCode Today's Challenge: 2666 — Allow One Function Call Today’s problem was all about controlling function execution — ensuring that a given function runs only once, no matter how many times it’s called afterward. Here’s my solution 👇 var once = function(fn) { let called = false; return function (...args) { if (!called) { called = true; return fn(...args); } }; }; This challenge reinforces the importance of closures in JavaScript — allowing us to preserve state (called) between function invocations. It’s a simple yet powerful example of how closures give us fine-grained control over function behavior. Try it out here: https://lnkd.in/g6WC5mu7 #JavaScript #LeetCode #CodingChallenge #LearningJourney #30DaysOfCode #Closures #Functions #Programming #Developers #Learning
To view or add a comment, sign in
-
-
Day 14 of #30DaysOfJavaScript on LeetCode Today's Challenge: 2715 — Timeout Cancellation This problem focused on controlling asynchronous behavior — specifically, learning how to cancel a scheduled function call using setTimeout() and clearTimeout(). Here’s my solution 👇 var cancellable = function(fn, args, t) { let time = setTimeout(() => { fn(...args) }, t) return function() { clearTimeout(time) } } The challenge helped me understand how JavaScript manages timers and how we can cancel pending asynchronous tasks before they execute. Try it out here: https://lnkd.in/g6WC5mu7 #JavaScript #LeetCode #CodingChallenge #AsyncAwait #Promises #30DaysOfCode #Developers #Programming #LearningJourney
To view or add a comment, sign in
-
-
Unleash the Potential of Array Methods!** Stop writing verbose loops! JavaScript Array methods like `map`, `filter`, and `reduce` are your secret weapons. Write cleaner, more readable, & more performant code. They're not just shorter; they're a whole new level of elegance. What's one Array method that changed the way YOU code? Share your experiences! 👇 #javascript #arrays #codingtips #webdev #programming
To view or add a comment, sign in
-
Just published a beginner-friendly guide to understanding function binding in JavaScript! If you've ever wondered why `this` becomes undefined in callbacks, this article breaks it down with simple examples. Perfect for developers learning JavaScript or anyone who needs a refresher on bind() 💻 https://lnkd.in/dmAU3Rhc #JavaScript #WebDevelopment #Programming #Coding
To view or add a comment, sign in
-
Day 12 of #30DaysOfJavaScript on LeetCode Today's Challenge: 2723 — Add Two Promises Today’s problem was all about working with asynchronous operations in JavaScript — specifically how to handle multiple promises efficiently. Here’s my solution 👇 var addTwoPromises = async function(promise1, promise2) { const [a, b] = await Promise.all([promise1, promise2]); return a + b; }; This challenge reinforced how Promise.all() allows us to run promises in parallel, waiting for all of them to resolve before proceeding — a key concept in optimizing asynchronous workflows. It’s a clean and elegant way to handle multiple async tasks without unnecessary delays. Try it out here: https://lnkd.in/g6WC5mu7 #JavaScript #LeetCode #CodingChallenge #LearningJourney #30DaysOfCode #AsyncAwait #Promises #Programming #Developers #Learning
To view or add a comment, sign in
-
-
Day 13 of #30DaysOfJavaScript on LeetCode Today's Challenge: 2621 — Sleep Today’s challenge explored the basics of asynchronous timing in JavaScript — creating a custom sleep() function that pauses execution for a given duration using promises. Here’s my solution 👇 async function sleep(millis) { return new Promise(resolve => setTimeout(resolve, millis)); } This simple problem reinforces how promises and setTimeout() work together to handle delays asynchronously. Understanding this helps build a solid foundation for mastering async behavior in JavaScript. Try it out here: https://lnkd.in/g6WC5mu7 #JavaScript #LeetCode #CodingChallenge #LearningJourney #30DaysOfCode #AsyncAwait #Promises #Programming #Developers #Learning
To view or add a comment, sign in
-
-
🚀 JavaScript Closures — A Little Magic Trick! Imagine… a function that can remember its past values, even after it has finished running! 😲 That’s the magic of closures. In this 3-frame short video, you’ll see: ✨ What a closure is ✨ How an inner function can access outer variables ✨ Why we use closures #JavaScript #CodingMagic #WebDev #Closures #TechFun #Programming #DeveloperLife
To view or add a comment, sign in
-
Every JavaScript developer has hit that confusing moment when var starts acting like it has a mind of its own 😅 I still remember spending hours debugging a scope issue… only to find out that var is function-scoped, while let and const are block-scoped. That’s exactly what we break down in our new Instagram video a quick visual guide to mastering var, let, and const, so you’ll never get caught off guard again. 📹 Check out the full video on our Instagram-> link in the comments 👇 #JavaScript #WebDevelopment #CodingTips #LearnToCode #FrontendDevelopment #Programming #TechEducation
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
Oh that's exactly how I've been thinking about async control lately. Promise.race() is such a clean way to handle timeouts without getting messy with the logic... your solution really shows how powerful promise composition can be