AsyncAwait icon indicating copy to clipboard operation
AsyncAwait copied to clipboard

Await completion of another coroutine

Open okkero opened this issue 8 years ago • 7 comments

It would be nice if there was an easy builtin way to await on another async coroutine. As is possible in C# with Task and Task<T>.

okkero avatar Nov 25 '16 16:11 okkero

You can use many await calls in one async block, like

async {
    val result1 = await { getResult1() }
    val result2 = await { getResult2(result1) }
}

Can this work for your case?

pilgr avatar Nov 25 '16 17:11 pilgr

Sadly no. Imagine a scenario where a method foo runs an async coroutine, and then another method foo2 rund another async coroutine, within which is a call to foo. I need a way for foo2's async coroutine to await foo's coroutine to completely finish, before continuing. C# handles this case nicely.

okkero avatar Nov 25 '16 19:11 okkero

Could you provide a code sample in C#?

On Nov 25, 2016 19:05, "Ole Kristian Sandum" [email protected] wrote:

Sadly no. Imagine a scenario where a method foo runs an async coroutine, and then another method foo2 rund another async coroutine, within which is a call to foo. I need a way for foo2's async coroutine to await foo's coroutine to completely finish, before continuing. C# handles this case nicely.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/metalabdesign/AsyncAwait/issues/9#issuecomment-263011912, or mute the thread https://github.com/notifications/unsubscribe-auth/AAaNIdEvuuQbyuCZgnPGKKlTy7JY9dQJks5rBzGSgaJpZM4K8mSb .

pilgr avatar Nov 25 '16 20:11 pilgr

        static async Task<string> foo()
        {
            //Does some long async stuff here
            //Uses await when it needs to

            return "Hello";
        }

        static async Task foo2()
        {
            //Some stuff here
            var fooResult = await foo();
            //Everything from this line happens AFTER foo() has finished its async execution
        }

So, here there is a lot of syntactic sugar. Kotlin doesn't have as much syntactic sugar, but bear with me. Here, foo2 is effectively waiting for foo to finish its entire execution before continuing. As far as I know (and I've tried) there isn't any easy way to do this using this lib.

Kotlin:

fun foo(): String { //Future<String>, maybe?
    async {
        //Does some long async stuff here
        //Uses await when it needs to

        //How do I return my result???
    }
}

fun foo2() {
    async {
        //Some stuff here
        val fooResult = await { foo() }
        //I can do this, sure (given foo can actually return its result), but this call to foo() returns right away. I.e, I cannot await its completion
    }
}

okkero avatar Nov 25 '16 20:11 okkero

I have to clarify that code in async block still runs in UI thread. Only a code in await block runs in a background thread. So in your case, by calling await { foo() }, do you expect the whole foo() to be completed (including UI code + background code + UI code again)?

If we rewrite foo() to be the normal function, the code will work like in C# example above.

fun foo(): String {
    return result
}

But I see your point and can imagine the cases where such possibility could be helpful. Like having a function to upload a file and display progress and result in UI, and also have a possibility to call this function in a loop in other function for multiple files. For now lib doesn't support this case.

pilgr avatar Nov 25 '16 23:11 pilgr

I have to clarify that code in async block still runs in UI thread. Only a code in await block runs in a background thread.

I realise this.

So in your case, by calling await { foo() }, do you expect the whole foo() to be completed (including UI code + background code + UI code again)?

Yes. This is exactly what I want to achieve. Only after all what you mentioned. After every single thing that foo could possibly do and wait for. After everything is finished, then foo2 will continue.

For now lib doesn't support this case.

Can I expect to see this implemented in the near in the future?

okkero avatar Nov 25 '16 23:11 okkero

Hope so, but I can't promise anything. Submit your PR :)

pilgr avatar Nov 25 '16 23:11 pilgr