Created
April 9, 2023 14:06
-
-
Save tavurth/fe3f5d8ae831a04dc6be67602385b685 to your computer and use it in GitHub Desktop.
Promise.all for Godot 4
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 Promise | |
extends Resource | |
signal completed(results) | |
var _funcs: Array[Callable] | |
var _completed := false | |
var _results: Dictionary | |
func _init(funcs: Array[Callable]): | |
_funcs = funcs | |
for idx in range(len(_funcs)): | |
_call_func(_funcs[idx], idx) | |
func _call_func(f: Callable, idx: int) -> void: | |
var result = await f.call() | |
_funcs.erase(f) | |
_results[idx] = result | |
if _funcs.is_empty() and not _completed: | |
var keys = _results.keys() | |
keys.sort() | |
var to_emit = [] | |
for i in keys: | |
to_emit.append(_results[i]) | |
completed.emit(to_emit) | |
_completed = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment