Skip to main content

Promise Throttle

SENIOR

Promise Throttle

Create a throttle wrapper for async functions with time-based rate limiting.

Requirements:

  • Implement throttleAsync that wraps an async function
  • Ensure function executes at most once per interval milliseconds
  • Queue calls that happen during cooldown
  • Execute queued calls after cooldown expires
  • Support cancellation of queued calls

Example:

const apiCall = async (data) => {
  return `Processed: ${data}`;
};

const throttled = throttleAsync(apiCall, 1000);

throttled('A'); // Executes immediately
throttled('B'); // Queued, executes after 1s
throttled('C'); // Queued, executes after 2s

Examples:

Input 1:{"interval":100,"calls":[1,2,3]}
Output 1:[2,4,6]

Loading editor...

Run your code to see results

Click the Run button above