How can we join multiple coroutines? Await waits only on a single coroutine

:bust_in_silhouette: Reply From: kienhoang

Hi, I made it by creating a small utility called Counter and using lambda functions to make different coroutine blocks.:

class_name Counter

signal completed

var _counter: int
var _total: int


func _init(p_total: int):
    _total = p_total
    _counter = 0


func submit():
    _counter = _counter + 1
    if _counter == _total:
        completed.emit()

In my main functions:

func main():
    var jobs = Counter.new(3) # I have 3 jobs here

    var job_1 = func():
        await download_coroutine_1
        jobs.submit()

    job_1.call()

    var job_2 = func():
        await download_coroutine_2
        jobs.submit()

    job_2.call()

    var job_3 = func():
        await download_coroutine_3
        jobs.submit()
    
    job_3.call()

    await jobs.completed

Thanks for this feedback. I think that could work.

In your example, don’t you still need to invoke the lambdas? And do you do that without awaits so you only block at the bottom?

‘’’
job_1
job_2
job_3
await jobs.completed
‘’’

pseudonymous_person | 2023-02-26 14:21

Oh yes, that’s true. Thanks

kienhoang | 2023-02-26 14:46