async-cache-0.js
✓ Published0🌍 Public
Aalessioalex
Last edited Nov 22, 2014
Created on Nov 22, 2014
This example demonstrates a caching mechanism for asynchronous functions, showing how repeated calls can bypass a slow database lookup. It defines `cachify`, a wrapper that takes an original async function and a cache-fetching function, returning a new function to use instead. The code uses `setTimeout` to simulate random 1–2 second database delays and a near-instant cache response, with data stored in a plain in-memory `users` object. The example compares timing via `console.time` and `console.timeEnd`, illustrating the performance difference between direct database access and cached retrieval.
AI-generated description