Skip to content

Instantly share code, notes, and snippets.

@tavurth
Created April 9, 2023 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tavurth/fe3f5d8ae831a04dc6be67602385b685 to your computer and use it in GitHub Desktop.
Save tavurth/fe3f5d8ae831a04dc6be67602385b685 to your computer and use it in GitHub Desktop.
Promise.all for Godot 4
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