Skip to content

CancelToken / CancelSource ​

The two types for canceling requests, created by createCancelSource() (same mental model as axios CancelToken.source()): attach the token to the cancelToken field of RequestConfig, then cancel(reason) cancels the associated requests.

uts
/** Cancel token */
export type CancelToken = {
	/** Whether already canceled */
	canceled: boolean
	/** Cancellation reason; null when not canceled */
	reason: string | null
}

/** Cancel source (returned by createCancelSource, same mental model as axios CancelToken.source()) */
export type CancelSource = {
	/** Associated token (attach to RequestConfig.cancelToken) */
	token: CancelToken
	/** Cancels all in-flight requests associated with the token */
	cancel: (reason: string | null) => void
}

CancelToken Fields ​

NameTypeDescription
canceledbooleanWhether already canceled. Set to true after cancel(); from then on, new requests associated with the token are intercepted before sending
reasonstring | nullCancellation reason; null when not canceled. Reflected in the ERR_CANCELED errMsg (请求已取消:<reason>, i.e. "request canceled: <reason>")

CancelSource Fields ​

NameTypeDescription
tokenCancelTokenThe cancel token, attached to RequestConfig.cancelToken
cancel(reason: string | null) => voidCancels all in-flight requests associated with the token (deterministic abort() inside the library); repeated calls are idempotent

Capability boundary of self-built tokens

CancelToken is a plain object type, so business code can build { canceled: true, reason: 'x' } itself — but such tokens are not in the library's registry and only get the pre-send check (already canceled → ERR_CANCELED without a network call); the deterministic in-flight abort requires a token created via createCancelSource().

Examples ​

uts
import { request, createCancelSource } from '@/uni_modules/ux-request'
import type { RequestError } from '@/uni_modules/ux-request'

const source = createCancelSource()

request<UTSJSONObject>({ url: '/api/heavy', cancelToken: source.token }).then((res) => {
	console.log(res.statusCode)
}).catch((err) => {
	const e = err as RequestError
	if (e.code == 'ERR_CANCELED') {
		console.log(source.token.canceled) // true
		console.log(source.token.reason)   // 'page unloaded'
	}
})

setTimeout(() => {
	source.cancel('page unloaded')
}, 100)

Released under the MIT License.