{"library":"memoized-node-fetch","title":"Memoized Node Fetch","description":"memoized-node-fetch is a JavaScript/TypeScript utility that wraps `node-fetch` (or any `fetch`-like function) to implement request promise memoization. Its primary function is to return the *same promise* for identical concurrent requests until that promise resolves, preventing redundant calls to external APIs. Unlike a traditional data cache, this library specifically caches the promise object itself, not the resolved data, and the cache is cleared immediately upon resolution or rejection. The current stable version is 1.1.5. While not strictly scheduled, the project appears to follow an as-needed release cadence for bug fixes and minor improvements. Its key differentiator from state management libraries like React Query or SWR is its focus on *deduplicating in-flight requests* rather than long-term data caching, making it complementary to such libraries when used as their underlying fetcher. It hashes the URL and request options to determine if requests are identical.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install memoized-node-fetch"],"cli":null},"imports":["import memoizedNodeFetch from 'memoized-node-fetch';","const memoizedNodeFetch = require('memoized-node-fetch');","import memoizedNodeFetch from 'memoized-node-fetch';\nconst fetch = memoizedNodeFetch();\n// 'fetch' is now the memoized fetch function; its type is inferred as (url: RequestInfo, options?: RequestInit) => Promise<Response>."],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import memoizedNodeFetch from 'memoized-node-fetch';\n\n// Get a memoized fetch instance. This creates a new promise cache.\nconst fetch = memoizedNodeFetch();\n\n(async () => {\n    console.log('Initiating two identical requests concurrently...');\n    const startTime = Date.now();\n\n    // Both calls to fetch for the same URL will return the same promise while it's in flight.\n    const fetch1 = fetch('https://jsonplaceholder.typicode.com/todos/1');\n    const fetch2 = fetch('https://jsonplaceholder.typicode.com/todos/1');\n\n    // Verify that both calls indeed return the exact same promise object.\n    console.log(`Are fetch1 and fetch2 the same promise object? ${fetch1 === fetch2}`);\n\n    // Wait for the first (and only) actual network request to resolve.\n    const res1 = await fetch1; \n    // This will resolve immediately after fetch1, as it's the same promise.\n    const res2 = await fetch2;\n\n    const endTime = Date.now();\n    console.log(`Concurrent requests completed in ${endTime - startTime}ms`);\n\n    // Log the JSON bodies, demonstrating the data consistency.\n    const data1 = await res1.json();\n    const data2 = await res2.json();\n\n    console.log('Data from fetch1:', data1);\n    console.log('Data from fetch2:', data2);\n\n    console.log('\\nDemonstrating non-caching after promise resolution...');\n    // After the promise resolves, it's removed from the cache. \n    // A new request will result in a new promise.\n    const fetch3 = fetch('https://jsonplaceholder.typicode.com/todos/1');\n    console.log(`Is fetch3 (after previous resolution) the same promise object as fetch1? ${fetch3 === fetch1}`);\n\n    const res3 = await fetch3;\n    const data3 = await res3.json();\n    console.log('Data from fetch3:', data3);\n})();","lang":"typescript","description":"Demonstrates how concurrent identical requests return the same promise. It also clarifies that the cache is cleared once the promise resolves, preventing data caching.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}