From 1da36968f6552d1ee6d6e3385d0dfadead0227c4 Mon Sep 17 00:00:00 2001 From: GamefoldTV <92638771+GamefoldTV@users.noreply.github.com> Date: Tue, 6 Feb 2024 18:11:15 +0700 Subject: [PATCH] Create 08 --- 08_coroutines/08 | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 08_coroutines/08 diff --git a/08_coroutines/08 b/08_coroutines/08 new file mode 100644 index 0000000..1a4b1b6 --- /dev/null +++ b/08_coroutines/08 @@ -0,0 +1,24 @@ +import io.ktor.client.* +import io.ktor.client.request.* +import kotlinx.coroutines.async +import kotlinx.coroutines.runBlocking + +data class PostWithAuthor( + val post: Post, + val author: Author +) + +fun main() = runBlocking { + val client = HttpClient() + val posts = client.get>("http://your-server.com/api/posts") + val postsWithAuthors = posts.map { post -> + async { + val author = client.get("http://your-server.com/api/authors/${post.authorId}") + PostWithAuthor(post, author) + } + }.awaitAll() + + postsWithAuthors.forEach { println(it) } + + client.close() +}