Skip to content

Cancellation

Pass a CancellationToken via Options.cancellation_token and call token.cancel() from anywhere — another coroutine, a signal handler, a button press. The polling loop checks the token between iterations and, once cancelled, abandons the request and returns a Response with error.kind == CANCELLED. Reach for cancellation when the caller decides a response is no longer needed.

class_name Game
extends Node2D

var _token: C3Http.CancellationToken


func _ready() -> void:
    cancel_button.pressed.connect(_on_cancel_pressed)
    _load_profile()


func _on_cancel_pressed() -> void:
    _token.cancel()


func _load_profile() -> void:
    _token = C3Http.CancellationToken.new()
    var opts := C3Http.Options.new()
    opts.cancellation_token = _token

    var res := await C3Http.request(
        "https://api.example.com/profile",
        PackedStringArray(),
        HTTPClient.METHOD_GET,
        "",
        opts
    )
    if not res.ok and res.error.kind == C3Http.RequestError.Kind.CANCELLED:
        print("Cancelled.")

SSE requests are cancelled the same way: pass a CancellationToken via Options.cancellation_token, and calling token.cancel() stops the stream early — the await resolves with res.ok == false and res.error.kind == C3Http.RequestError.Kind.CANCELLED.