Cancelable Function with Timeout Challenge in JavaScript

Day 14/30 – Cancelable Function with Timeout in JavaScript Challenge ⏳❌ | Async 💻🚀 🧠 Problem: Create a function that: Delays execution of fn by t milliseconds Returns a cancel function (cancelFn) If cancelFn is called before the delay ends → execution is canceled If not canceled → fn runs with the given arguments ✨ What this challenge teaches: Timer management using setTimeout Cancellation patterns in async JavaScript Controlling execution flow — a key skill for real-world apps This concept is used in: ⚡ Debouncing & throttling ⚡ API request cancellation ⚡ User interaction handling ⚡ Performance optimization Example 1: Input: fn = (x) => x * 5, args = [2], t = 20 Output: [{"time": 20, "returned": 10}] Explanation: const cancelTimeMs = 50; const cancelFn = cancellable((x) => x * 5, [2], 20); setTimeout(cancelFn, cancelTimeMs); The cancellation was scheduled to occur after a delay of cancelTimeMs (50ms), which happened after the execution of fn(2) at 20ms. Constraints: fn is a function args is a valid JSON array 1 <= args.length <= 10 20 <= t <= 1000 10 <= cancelTimeMs <= 1000 💬 Where would you use cancelable logic in a real project? #JavaScript #30DaysOfJavaScript #CodingChallenge #AsyncJavaScript #Closures #JSLogic #LeetCode #WebDevelopment #LearnToCode #CodeEveryday #DeveloperJourney #Programming #TechCommunity JavaScript cancelable function setTimeout cancel execution Async function cancellation JS JavaScript debounce logic LeetCode JavaScript solution JS interview questions Advanced JavaScript concepts Daily coding challenge

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories