Skip to content

client API methods abort

hannsolo edited this page May 28, 2020 · 1 revision

Aborting client operations

In order to be able to abort a client operation before it succeeds or fails, you need to pass an APIAbortController instance in the options when calling an operation.

Use client.getNewAbortController() to receive a controller and pass it in the IOptions parameter (or one of its descendants) of the operation to run.

In order to cancel an operation call the abort() method of the controller:

const abortController = client.getNewAbortController();
const options = {"abortController": abortController};
const listOptions = {
 "recursive":true,
 "abortController":abortController
};

client.login("user", "password", options)
.then( () => {
   client.listServer("images/", listOptions)
   .then( (success) => {
      console.log("listServer finished with result " + success);
    })
    .catch(console.error)
    .finally( () => {
      client.logout(options)
      .catch(console.error)
    });
})
.catch(console.error);

setTimeout( () => {
  // abort the process
  abortController.abort();
}, 100);

Once the operation has been aborted the catch(error) part of the operation in progress will be called. The error.type is "aborted".

Aborting processes running in parallel

While re-using an abortController for multiple operations running sequentially is fine, you must use individual abortControllers for operations running in parallel.

The reason is that the abortController creates a new cancelToken for each HTTP request. If you use the same cancel token for processes running in parallel, only the one that initiated the last HTTP request will be aborted while all others will continue to run.

getNewAbortController(): APIAbortController

Returns a new abort controller instance. The abort controller instance can be passed to IOptions or its descendants like IHTTPOptions in order to be able to abort a request.

APIAbortController.abort(): boolean

Abort the operation the APIAbortController has been passed to. abort() implicitly releases the APIAbortController.

Returns false if the operation has already been canceled before, otherwise true.

APIAbortController.getAborted(): boolean

Returns true if abort() has been called.

APIAbortController.reset(): boolean

Resets the abortController. Call this method after receiving an abort error to continue using the abortController.

Clone this wiki locally