
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
memoized-node-fetch
Advanced tools
A wrapper around node-fetch that caches the request's promise before resolving.
A wrapper around node-fetch (or any other fetch-like function) that returns a single promise until it resolves.
Sometimes, you have to interface with an API that doesn't respond fast enough. Moreover, you might perform the same request multiple times. So:
Return the same promise for the same exact requests until they resolve. This is more useful when you interface with stateless APIs, where you just consume data.
User (1) makes a request to the backend. The backend then performs a request to a third-party API and then before it resolves, user (2) makes another request to the backend. The backend then needs to perform the same request, as before, to the third-party API. With this package, instead of performing a new request, you can access and use the same promise for user's (1) request and have user (2) wait for the same request's resolution. This should shorten the wait time for user (2).
This API is a wrapper around node-fetch.
Install the module: $ npm i memoized-node-fetch
import memoizedNodeFetch from 'memoized-node-fetch';
const fetch = memoizedNodeFetch();
(async () => {
const fetch1 = fetch('https://jsonplaceholder.typicode.com/todos/1');
const fetch2 = fetch('https://jsonplaceholder.typicode.com/todos/1');
// This should return true because both requests return the same promise.
console.log(fetch1 === fetch2);
const res1 = await fetch1;
const res2 = await fetch2;
console.log(await res1.json());
console.log(await res2.json());
})();
No. This package only caches the promise until it resolves. After the promise resolves, it is removed from the cache, along with the data returned.
The parameters of the two fetch functions are compared (the url and the RequestOptions), the specific key used for comparing the requests is:
const key = stringToHash(url.toString() + JSON.stringify(options));
The parameters of the request are hashed and stored on a map.
Of course, you can use your own fetch
like this:
function myOwnFetch(url: RequestInfo, options?: RequestInit | undefined): Promise<Response> {
/* bla bla bla */
}
const fetch = memoizedNodeFetch(myOwnFetch);
/* Use the fetch... */
Yes! Each time you run the factory function, a new promise-cache is created.
No. For most cases, you shouldn't use this library instead of react-query or swr. Rather you could use it in tandem with those libraries by substituting the fetcher function with this. Those libraries, although they implement caching, they don't implement it while the fetch is loading (so if you perform the request two times, you'll get two different promises).
Thanks goes to these wonderful people (emoji key):
Nikos Polykandriotis 💻 | Fernando van Loenhout 💻 | Bonjour Comosava 📖 |
This project follows the all-contributors specification. Contributions of any kind welcome!
FAQs
A wrapper around node-fetch that caches the request's promise before resolving.
The npm package memoized-node-fetch receives a total of 22,479 weekly downloads. As such, memoized-node-fetch popularity was classified as popular.
We found that memoized-node-fetch demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.