coopernurse / node-pool

Generic resource pooling for node.js
2.37k stars 258 forks source link

AutoStart Completion Callback #307

Closed nyteshade closed 5 months ago

nyteshade commented 5 months ago

If autostart is provided in the config, allow an option that takes a callback to be invoked when the minimum number of connections are primed. Or alternatively, provide a promise that can be waited on to achieve the same effect.

const deferred = Promise.withResolvers()
const myPool = new genericPool.createPool(factory, {
  ...opts, 
  autostart: true, 
  started: deferred.resolve,      // this can be a () => {} as well but am using 
                                  // Promise.withResolvers().resolve instead to allow for await
});

await deferred.promise; // now the pool has met minimum connections

and/or

const myPool = new genericPool.createPool(factory, {
  ...opts, 
  autostart: true,
});
await myPool.started; // now the pool has met minimum connections

The argument for both is that some libraries that use this code (such as the Snowflake SDK) do not expose the GenericPool directly.

nyteshade commented 5 months ago

I had a PR just about ready when I saw Pool.ready(). Thanks.