Aborting a request

edit

When using the callback style API, the function will also return an object that allows you to abort the API request.

// calback API
const request = client.search({
  index: 'my-index',
  body: {
    query: {
      match: { hello: 'world' }
    }
  }
}, {
  ignore: [404],
  maxRetries: 3
}, (err, { body }) => {
  if (err) console.log(err)
})

request.abort()

Aborting a request with the promise style API is not supported, but you can easily achieve that with convenience wrapper.

function abortableRequest (params, options) {
  var request = null
  const promise = new Promise((resolve, reject) => {
    request = client.search(params, options, (err, result) => {
      err ? reject(err) : resolve(res)
    })
  })
  return {
    promise,
    abort: () => request.abort()
  }
}

const request = abortableRequest({
  index: 'my-index',
  body: {
    query: {
      match: { hello: 'world' }
    }
  }
}, {
  ignore: [404],
  maxRetries: 3
})

request.abort()
// access the promise with `request.promise.[method]`