diff --git a/src/Pool.ts b/src/Pool.ts index 9bcc90c..c95c252 100644 --- a/src/Pool.ts +++ b/src/Pool.ts @@ -16,7 +16,7 @@ export interface PoolOptions { createRetryIntervalMillis?: number; reapIntervalMillis?: number; log?: (msg: string) => any; - validate?: (resource: T) => boolean; + validate?: (resource: T) => Promise | boolean; propagateCreateError?: boolean; } @@ -41,7 +41,7 @@ export class Pool { protected log: (msg: string, level: 'warn') => any; protected creator: CallbackOrPromise; protected destroyer: (resource: T) => any; - protected validate: (resource: T) => boolean; + protected validate: (resource: T) => Promise | boolean; protected eventId: number; protected emitter = new EventEmitter(); @@ -474,13 +474,10 @@ export class Pool { return this.free.length > 0 && this.pendingAcquires.length > 0; } - _validateResource(resource: T) { - try { - return Promise.resolve(this.validate(resource)); - } catch (err) { - // prevent leaking of sync exception - return Promise.reject(err); - } + async _validateResource(resource: T) { + const result = await this.validate(resource); + + return result; } _shouldCreateMoreResources() { diff --git a/src/tarn.ts b/src/tarn.ts index 0aa5053..3da23d1 100644 --- a/src/tarn.ts +++ b/src/tarn.ts @@ -1,7 +1,7 @@ -import { Pool } from './Pool'; +import { Pool, PoolOptions } from './Pool'; import { TimeoutError } from './TimeoutError'; -export { Pool, TimeoutError }; +export { Pool, PoolOptions, TimeoutError }; module.exports = { Pool,