
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
promise-inflight
Advanced tools
One promise for multiple requests in flight to avoid async duplication
The promise-inflight npm package is designed to ensure that a promise with a specific key is only in flight once. If multiple requests are made with the same key, they will all receive the same promise, preventing duplicate operations. This is particularly useful for avoiding redundant network requests or expensive computations when the same operation might be triggered multiple times concurrently.
Single instance promise management
This feature ensures that for a given key, only one promise is in flight. If the same key is used in subsequent calls while the promise is still pending, the same promise is returned. This avoids executing the fetch function multiple times for the same data.
const inflight = require('promise-inflight');
async function fetchData(key, fetchFunction) {
return inflight(key, () => fetchFunction());
}
// Usage example
fetchData('user-data', () => fetch('https://api.example.com/user')).then(console.log);
p-memoize is a package that memoizes the result of a promise-returning or async function. It can cache results based on the input arguments and offers more extensive configuration options for cache management compared to promise-inflight, which focuses solely on managing concurrent identical requests.
Similar to p-memoize, async-memoize provides memoization for asynchronous functions. It supports multiple strategies for cache eviction and can handle concurrent execution more flexibly than promise-inflight, which is specifically tailored for handling cases where identical promises should not be executed multiple times concurrently.
One promise for multiple requests in flight to avoid async duplication
const inflight = require('promise-inflight')
// some request that does some stuff
function req(key) {
// key is any random string. like a url or filename or whatever.
return inflight(key, () => {
// this is where you'd fetch the url or whatever
return Promise.delay(100)
})
}
// only assigns a single setTimeout
// when it dings, all thens get called with the same result. (There's only
// one underlying promise.)
req('foo').then(…)
req('foo').then(…)
req('foo').then(…)
req('foo').then(…)
Tests!
FAQs
One promise for multiple requests in flight to avoid async duplication
The npm package promise-inflight receives a total of 9,634,787 weekly downloads. As such, promise-inflight popularity was classified as popular.
We found that promise-inflight 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
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.