diff --git a/lib/Transport.d.ts b/lib/Transport.d.ts index 979393a60..3464e2bad 100644 --- a/lib/Transport.d.ts +++ b/lib/Transport.d.ts @@ -120,6 +120,7 @@ export interface TransportRequestCallback { export interface TransportRequestPromise extends Promise { abort: () => void; + finally(onFinally?: (() => void) | undefined | null): Promise; } export interface TransportGetConnectionOptions { diff --git a/lib/Transport.js b/lib/Transport.js index a61ce0d3a..c808c51bc 100644 --- a/lib/Transport.js +++ b/lib/Transport.js @@ -163,6 +163,9 @@ class Transport { request.abort() debug('Aborting request', params) return this + }, + finally (onFinally) { + return p.finally(onFinally) } } diff --git a/test/unit/api.test.js b/test/unit/api.test.js index f08422221..b5a7fbb57 100644 --- a/test/unit/api.test.js +++ b/test/unit/api.test.js @@ -124,6 +124,33 @@ test('Error (promises)', t => { }) }) +test('Finally method (promises)', t => { + t.plan(1) + + function handler (req, res) { + res.setHeader('Content-Type', 'application/json;utf=8') + res.end(JSON.stringify({ hello: 'world' })) + } + + buildServer(handler, ({ port }, server) => { + const client = new Client({ + node: `http://localhost:${port}` + }) + + const request = client.search({ + index: 'test', + q: 'foo:bar' + }) + + t.type(request.finally, 'function') + + request + .finally(() => { + server.stop() + }) + }) +}) + test('Abort method (callback)', t => { t.plan(3)