---
name: "up.network.abort"
area: "API"
kind: "JavaScript function"
parent: "up.network module"
url: "https://unpoly.com/api/up.network.abort"
---
<nav aria-label="Unpoly docs">[All docs](https://unpoly.com/index.md) · [up.network module](https://unpoly.com/api/up.network.md)</nav>

# up.network.abort([condition], [options])

Aborts pending [requests](https://unpoly.com/api/up.request.md) matching a condition.

> <nav aria-label="Guides">
>
> - [Learn: Aborting requests](https://unpoly.com/learn/aborting-requests.md)
>
> </nav>
>
> #### Important
>
> This is a low-level API for matching requests by their properties. If possible, use [`up.fragment.abort()`](https://unpoly.com/api/up.fragment.abort.md), which matches requests by screen region. Only when requests are aborted by screen region can components [react to being aborted](https://unpoly.com/api/up:fragment:aborted.md).

<nav aria-label="Contents">

#### Contents

- [Effects of aborting](#effects-of-aborting)
- [Aborting all requests](#aborting-all-requests)
- [Aborting a single request](#aborting-a-single-request)
- [Aborting requests matching a pattern](#aborting-requests-matching-a-pattern)
- [Aborting requests matching an arbitrary condition](#aborting-requests-matching-an-arbitrary-condition)
- [Aborting requests targeting a fragment or layer](#aborting-requests-targeting-a-fragment-or-layer)
- [Parameters](#parameters)

</nav>

## Effects of aborting {#effects-of-aborting}

When an [`up.request()`](https://unpoly.com/api/up.request.md) is aborted, its returned promise rejects with an `up.Aborted` error:

```js
try {
  let response = await up.request('/path')
  console.log(response.text)
} catch (error) {
  if (error.name === 'AbortError') {
    console.log('Request was aborted: ' + error.message)
  }
}
```

Also the event [`up:request:aborted`](https://unpoly.com/api/up:request:aborted.md) will be emitted. The region-specific event [`up:fragment:aborted`](https://unpoly.com/api/up:fragment:aborted.md) will _not_ be emitted by this function. For this use [`up.fragment.abort()`](https://unpoly.com/api/up.fragment.abort.md) instead.

## Aborting all requests {#aborting-all-requests}

Without arguments, this will abort all pending requests:

```js
up.network.abort()
```

## Aborting a single request {#aborting-a-single-request}

To abort a given [`up.Request`](https://unpoly.com/api/up.Request.md) object, pass it as the first argument:

```js
let request = up.request('/path')
up.network.abort(request)
```

## Aborting requests matching a pattern {#aborting-requests-matching-a-pattern}

To abort all requests matching an [URL pattern](https://unpoly.com/api/url-patterns.md), pass it as the first argument:

```js
up.network.abort('/path/*')
```

## Aborting requests matching an arbitrary condition {#aborting-requests-matching-an-arbitrary-condition}

To abort all requests matching an arbitrary condition, pass a function that takes a request and returns a boolean value. Unpoly will abort all requests for which the given function returns `true`. For example, to abort all requests with an HTTP method of `GET`:

```js
up.network.abort((request) => request.method == 'GET')
```

## Aborting requests targeting a fragment or layer {#aborting-requests-targeting-a-fragment-or-layer}

Use [`up.fragment.abort()`](https://unpoly.com/api/up.fragment.abort.md).

---

## Parameters {#parameters}

### [condition=true] {#condition}

(optional)

A condition that controls which requests to abort.

If set to a string, it is interpreted as a [URL pattern](https://unpoly.com/api/url-patterns.md). All requests matching that pattern will be aborted.

If set to an [`up.Request`](https://unpoly.com/api/up.Request.md) object, that one request is aborted.

If set to a function, it will be called for each pending request. All requests for which the function returns `true` will be aborted.

If set to `true`, all pending requests are aborted.

Type: string | Function([up.Request](https://unpoly.com/api/up.Request.md)): boolean | [up.Request](https://unpoly.com/api/up.Request.md) | boolean

### [options.reason] {#options.reason}

(optional)

A reason for why the request was aborted.

If omitted, a generic reason like `"Aborted request to GET /path"` will be used.

The reason will be set as the error's message.

Type: string

### [options.except] (Experimental) {#options.except}

(optional)

An [`up.Request`](https://unpoly.com/api/up.Request.md) that should not be aborted even if it matches the given `condition`.

Type: [up.Request](https://unpoly.com/api/up.Request.md)

