You can disable exiting the application when requested and exit it when you have saved the needed stuff.
extends Node
func _ready() -> void:
get_tree().auto_accept_quit = false
func _notification(what: int) -> void:
if what == NOTIFICATION_WM_CLOSE_REQUEST:
# do save stuff here
get_tree().quit()