I am looking for a way to efficiently manage multiple costly server calls by continuously invoking a function that accepts a key and returns a promise containing an object. This object is guaranteed to have the requested key along with additional values, including unrelated keys. The desired behavior of this function would be:
- When first called, it should generate a promise.
- Subsequent calls should accumulate keys to be sent to the server.
- All calls will return the same promise until there has been 100ms of inactivity.
- If no new calls are made within 100ms, all accumulated keys should be sent to the server for processing.
- If a new call is made while waiting for a response from the server, it should initiate a fresh promise with its own set of pending keys.
- Upon receiving the response from the server, the pending promise should be resolved.
Is there any npm package available that can assist with implementing this functionality, or should I develop it from scratch?