Skip to content

cache / cachify

cache and cachify are aliases for memoize. They create a cached version of an async function, storing results by argument key.

See memoize for full documentation.

Root package:

import { cache, cachify } from "@vgerbot/async";

Module subpath:

import { cache, cachify } from "@vgerbot/async/utils";

Leaf subpath:

import { cache } from "@vgerbot/async/utils/cache";
import { cache } from "@vgerbot/async";
const fetchUser = cache(async (userId: string, token) => {
const res = await token.wrap(fetch(`/api/users/${userId}`));
return res.json();
});
// First call fetches
const user1 = await fetchUser("123").promise;
// Second call returns cached result
const user2 = await fetchUser("123").promise;
console.log(user1 === user2); // true
  • memoize — the primary API (full documentation).
  • once — executes a function only once (no argument-based caching).