function asyncThrottle<TFn>(fn, initialOptions): (...args) => Promise<undefined | ReturnType<TFn>>
function asyncThrottle<TFn>(fn, initialOptions): (...args) => Promise<undefined | ReturnType<TFn>>
Definiert in: async-throttler.ts:500
Erstellt eine asynchrone, gedrosselte Funktion, die begrenzt, wie oft die Funktion ausgeführt werden kann. Die gedrosselte Funktion wird höchstens einmal pro Wartezeit ausgeführt, auch wenn sie mehrmals aufgerufen wird. Wenn sie während der Ausführung aufgerufen wird, wartet sie, bis die Ausführung abgeschlossen ist, bevor sie den nächsten Aufruf plant.
Im Gegensatz zum nicht-asynchronen Throttler unterstützt diese asynchrone Version die Rückgabe von Werten aus der gedrosselten Funktion, was sie ideal für API-Aufrufe und andere asynchrone Operationen macht, bei denen Sie das Ergebnis des maybeExecute-Aufrufs anstelle des Setzens des Ergebnisses auf einer Zustandsvariable innerhalb der gedrosselten Funktion wünschen.
Fehlerbehandlung
Zustandsverwaltung
• TFn erbt von AnyAsyncFunction
TFn
Funktion
Versucht, die gedrosselte Funktion auszuführen. Das Ausführungsverhalten hängt von den Throttler-Optionen ab
Wenn seit der letzten Ausführung genügend Zeit vergangen ist (>= Wartezeit)
Wenn innerhalb der Wartezeit
...Parameters<TFn>
Promise<undefined | ReturnType<TFn>>
const throttled = new AsyncThrottler(fn, { wait: 1000 });
// First call executes immediately
await throttled.maybeExecute('a', 'b');
// Call during wait period - gets throttled
await throttled.maybeExecute('c', 'd');
const throttled = new AsyncThrottler(fn, { wait: 1000 });
// First call executes immediately
await throttled.maybeExecute('a', 'b');
// Call during wait period - gets throttled
await throttled.maybeExecute('c', 'd');
const throttled = asyncThrottle(async (value: string) => {
const result = await saveToAPI(value);
return result; // Return value is preserved
}, {
wait: 1000,
onError: (error) => {
console.error('API call failed:', error);
}
});
// This will execute at most once per second
// Returns the API response directly
const result = await throttled(inputElement.value);
const throttled = asyncThrottle(async (value: string) => {
const result = await saveToAPI(value);
return result; // Return value is preserved
}, {
wait: 1000,
onError: (error) => {
console.error('API call failed:', error);
}
});
// This will execute at most once per second
// Returns the API response directly
const result = await throttled(inputElement.value);
Ihre wöchentliche Dosis JavaScript-Nachrichten. Jeden Montag kostenlos an über 100.000 Entwickler geliefert.