coopernurse / node-pool

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

Feature: add pool.use method. #283

Closed tracker1 closed 2 years ago

tracker1 commented 3 years ago

Would improve usability to have an async use function that accepts a method that returns a promise in order to more easily scope the usage.

const result = await pool.use(async (client) => {
  // use client
  return "some result";
});
// rough implementation
pool.use = async function(fn) {
  const client = await pool.acquire();
  try {
    const result = await fn(client);
    pool.release(client);
    return result;
  } catch (error) {
    // destroy may not be appropriate, but probably safest action.
    pool.destroy(client);
    throw error;
  }
};
martin-randall-tanium commented 2 years ago

This was addressed in or before v3.4.2 - see ceff50d843f1569b43670f37ff205024955d21e5