Skip to content

Commit ee74d14

Browse files
committed
Merge branch 'master' of ssh://github.com/koush/AndroidAsync
2 parents 35f3e04 + 6523c56 commit ee74d14

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

AndroidAsync-Kotlin/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Support for Kotlin Coroutines in AndroidAsync and Ion
2+
3+
Adds coroutines support to AndroidAsync and [Ion](https://github.com/koush/ion).
4+
5+
Maven:
6+
```xml
7+
<dependency>
8+
<groupId>com.koushikdutta.async</groupId>
9+
<artifactId>androidasync-kotlin</artifactId>
10+
<version>(insert latest version)</version>
11+
</dependency>
12+
```
13+
14+
Gradle:
15+
```groovy
16+
dependencies {
17+
compile 'com.koushikdutta.async:androidasync-kotlin:<insert latest version>'
18+
}
19+
```
20+
21+
Since AndroidAsync and Ion operations all returned futures, you can simply call await() on them within a Kotlin suspend function.
22+
23+
```kotlin
24+
suspend fun getTheRobotsTxt() {
25+
val googleRobots = Ion.with(context)
26+
.load("https://google.com/robots.txt")
27+
.asString()
28+
.await()
29+
30+
val githubRobots = Ion.with(context)
31+
.load("https://github.com/robots.txt")
32+
.asString()
33+
.await()
34+
35+
return googleRobots + githubRobots
36+
}
37+
```
38+
39+
That's it!
40+
41+
But remember that the await() suspends, so if you want to fetch both robots.txt at the same time:
42+
43+
```kotlin
44+
suspend fun getTheRobotsTxt() {
45+
val googleRobots = Ion.with(context)
46+
.load("https://google.com/robots.txt")
47+
.asString()
48+
49+
val githubRobots = Ion.with(context)
50+
.load("https://github.com/robots.txt")
51+
.asString()
52+
53+
return googleRobots.await() + githubRobots.await()
54+
}
55+
```
56+

0 commit comments

Comments
 (0)