DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.

← Back to Questions
HardJavaScript

Implement Async Debounce with Promise Support

51 views

Problem Statement

Modern JavaScript is heavily async/await based. Implement a debounce that works seamlessly with Promises, allowing callers to await the debounced result.

Requirements

Implement an asyncDebounce(func, wait) function that:

  • Returns a function that returns a Promise
  • All callers within the wait period receive the same Promise
  • The Promise resolves/rejects based on the actual function execution
  • Supports cancel() which rejects pending promises

Example Usage

const searchAPI = async (query) => {
  const response = await fetch(`/api/search?q=${query}`);
  return response.json();
};

const debouncedSearch = asyncDebounce(searchAPI, 300);

// Multiple components can await the same search
async function SearchComponent() {
  try {
    const results = await debouncedSearch("react");
    console.log("Component 1:", results);
  } catch (e) {
    console.log("Search cancelled or failed");
  }
}

async function SearchSuggestions() {
  try {
    const results = await debouncedSearch("react"); // Same promise!
    console.log("Component 2:", results);
  } catch (e) {
    console.log("Search cancelled or failed");
  }
}

// Both components get the same results from one API call

Edge Cases

  • What if the async function throws?
  • How do you handle cancel with pending promises?
  • What about concurrent debounced calls to different functions?

Follow-up Questions

  1. How would you add request deduplication on top of this?
  2. Should cancelled promises reject or stay pending forever?

Sample Test Cases

Case 1
Input
{"delay": 500, "calls": ["a", "b", "c"], "callTimes": [0, 100, 200]}
Expected Output
{"resolved": "c","resolvedAt": 700}
Case 2
Input
[
  async (val) => {
    await new Promise(resolve => setTimeout(resolve, 50));
    return `Processed: ${val}`;
  },
  100,
  [
    { type: 'call', args: ['a'], delay: 0 },
    { type: 'call', args: ['b'], delay: 50 },
    { type: 'call', args: ['c'], delay: 120 },
    { type: 'call', args: ['d'], delay: 150 },
    { type: 'call', args: ['e'], delay: 280 }
  ]
]
Expected Output
[
  "Processed: b",
  "Processed: b",
  "Processed: d",
  "Processed: d",
  "Processed: e"
]
Case 3
Input
{"delay": 300, "calls": ["x"], "callTimes": [0]}
Expected Output
{"resolved": "x","resolvedAt": 300}
Case 4
Input
[
  async (val) => {
    await new Promise(resolve => setTimeout(resolve, 20));
    if (val === 'error') throw new Error('Test Error');
    return `Result: ${val}`;
  },
  50,
  [
    { type: 'call', args: ['one'], delay: 0 },
    { type: 'call', args: ['two'], delay: 10 },
    { type: 'call', args: ['error'], delay: 60 },
    { type: 'call', args: ['final'], delay: 70 }
  ]
]
Expected Output
[
  "Result: two",
  "Result: two",
  "Test Error",
  "Test Error"
]
Case 5
Input
[
  async (id) => {
    await new Promise(resolve => setTimeout(resolve, 10));
    return `Data for ${id}`;
  },
  100,
  [
    { type: 'call', args: [1], delay: 0 },
    { type: 'call', args: [2], delay: 50 },
    { type: 'cancel', delay: 70 },
    { type: 'call', args: [3], delay: 150 }
  ]
]
Expected Output
[
  "Debounce cancelled",
  "Debounce cancelled",
  "Data for 3"
]

No solutions yet

Be the first to share a solution for this question.

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Stats

Views
51
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering