Skip to main content

Cancel Example

View this page on the UpscalerJS website

Demonstrates how to cancel an inflight upscale request.

Open in Codesandbox.

Cancelling requests

The mechanism for cancelling inflight requests leverages the AbortSignal object.

To cancel a request, create an instance of an AbortController, and then cancel it.

import Upscaler from 'upscaler'
import imagePath from '/path/to/image.png'

const upscaler = new Upscaler()
const abortController = new AbortController()
upscaler.upscale(imagePath, {
signal: abortController.signal,
}).catch(abortError => {
console.log('UpscalerJS has been aborted', abortError)
})

// at some later point in time ...

abortController.abort()

When abort is triggered during an upscale request, an AbortError is thrown. (AbortError is exported from the core UpscalerJS package.)

Cancelling all requests

UpscalerJS provides a convenience method for cancelling all inflight requests at once:

upscaler.abort()

Calling this will cancel all inflight requests (and each will emit an AbortError).

This method can be convenient, particularly if only one upscale request is ever active at a time.