Created
March 15, 2023 19:58
-
-
Save xphere/72471588bcd5f3855274b0cc418ae3f5 to your computer and use it in GitHub Desktop.
Task.WhenAll and Task.WhenAny Godot 4.0 GDScript 2.0 implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class_name Task | |
# Call all tasks and wait for them to complete, even async ones | |
static func WhenAll(tasks: Array[Callable]) -> void: | |
var awaiter := _Awaiter.new() | |
await awaiter.wait(tasks, tasks.size()) | |
# Call all tasks and wait for at least one to complete, even async ones | |
static func WhenAny(tasks: Array[Callable]) -> void: | |
var awaiter := _Awaiter.new() | |
await awaiter.wait(tasks, 1) | |
class _Awaiter: | |
signal completed | |
var _pending := 0 | |
func wait(tasks: Array[Callable], pending: int) -> void: | |
_pending = pending | |
for task in tasks: | |
_call(task) | |
if _pending > 0: | |
await completed | |
func _call(task: Callable) -> void: | |
await task.call() | |
_pending -= 1 | |
if _pending <= 0: | |
completed.emit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment