Skip to content

Key function

Amphiluke edited this page Dec 1, 2025 · 3 revisions

“Key function” (keyFn) is a common concept used in several APIs provided by the async-aid library: Cacher API, Deduper API, Superseder API.

The primary purpose of the key function is to allow the user to identify logically independent asynchronous processes served by the same single cacher/deduper/superseder. This allows the wrapper function to differentiate (or group) asynchronous operations on the base of user-specified criteria. Typically, the differentiation criteria are derived from the passed arguments, but the user can essentially define any arbitrary logic.

A key function takes the same arguments as the original async function and returns a distinct key. If the key function returns different keys for two asynchronous processes, they are considered logically unrelated and are handled independently of each other. Otherwise, such asynchronous processes are treated together as indistinguishable, according to the logic of the corresponding API.

Again, the logic for determining a distinct key depending on the arguments (or whatever else) can be as complex as you need it to be. Anyway, only you know your application well enough to decide how to differentiate one specific asynchronous operation from another. Notice that the key function must not return undefined or null as a key.

You can find keyFn usage examples in the documentation on Cacher API, Deduper API, and Superseder API.

Clone this wiki locally