Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 26e79ba

Browse files
committedJan 12, 2023
Add async/await example
1 parent 67c4fb8 commit 26e79ba

File tree

6 files changed

+10434
-1405
lines changed

6 files changed

+10434
-1405
lines changed
 

‎examples/async-await/async-await.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import kotlinx.coroutines.*
2+
import kotlin.system.*
3+
4+
suspend fun sumParallel(): Int = coroutineScope {
5+
val a = async { f(100, 1) }
6+
val b = async { f(200, 5) }
7+
a.await() + b.await()
8+
}
9+
10+
suspend fun f(v: Int, s: Int): Int {
11+
println("Fetching $v...")
12+
delay(s * 1000L)
13+
return v
14+
}
15+
16+
fun main() = runBlocking {
17+
val result = sumParallel()
18+
println(result)
19+
}

‎examples/async-await/async-await.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use tokio::time::{sleep, Duration};
2+
3+
async fn sum_parallel() -> u64 {
4+
let a_future = f(100, 1);
5+
let b_future = f(200, 5);
6+
let (a, b) = futures::join!(a_future, b_future);
7+
return a + b
8+
}
9+
10+
async fn f(v: u64, s: u64) -> u64 {
11+
println!("Fetching {v}...");
12+
// delay
13+
sleep(Duration::from_secs(s)).await;
14+
return v
15+
}
16+
17+
#[tokio::main]
18+
async fn main() {
19+
let result = sum_parallel().await;
20+
print!("{}", result)
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Foundation
2+
3+
func sumParallel() async -> Int {
4+
async let a = f(100, 1)
5+
async let b = f(200, 5)
6+
return await a + b
7+
}
8+
9+
func f(_ v: Int, _ s: Int) async -> Int
10+
{
11+
print("Fetching \(v)...")
12+
// delay
13+
let ns = UInt64(s * 1_000_000_000)
14+
try! await Task.sleep(nanoseconds: ns)
15+
return v
16+
}
17+
18+
@main struct Main {
19+
static func main() async {
20+
let result = await sumParallel()
21+
print(result)
22+
}
23+
}

‎examples/async-await/async-await.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
async function sumParallel() {
2+
const aPromise = f(100, 1);
3+
const bPromise = f(200, 5);
4+
const [a, b] = await Promise.all([
5+
aPromise,
6+
bPromise,
7+
]);
8+
return a + b;
9+
}
10+
11+
async function f(v: number, s: number) {
12+
console.log(`Fetching ${v}...`);
13+
// delay
14+
await new Promise((resolve) => {
15+
setTimeout(resolve, s * 1000);
16+
});
17+
return v;
18+
}
19+
20+
sumParallel().then(console.log);

‎package-lock.json

Lines changed: 10316 additions & 1396 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/examples.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export const examples: Example[] = [
4242
__dirname +
4343
"/../examples/protocols-interfaces-traits/stack.rs",
4444
"utf-8"
45-
)
46-
}
45+
),
46+
},
4747
},
4848
{
4949
key: "collections",
@@ -68,8 +68,8 @@ export const examples: Example[] = [
6868
__dirname +
6969
"/../examples/collections/collections.rs",
7070
"utf-8"
71-
)
72-
}
71+
),
72+
},
7373
},
7474
{
7575
key: "higher-order-functions",
@@ -94,8 +94,8 @@ export const examples: Example[] = [
9494
__dirname +
9595
"/../examples/higher-order-functions/map.rs",
9696
"utf-8"
97-
)
98-
}
97+
),
98+
},
9999
},
100100
{
101101
key: "algebraic-data-types",
@@ -120,7 +120,33 @@ export const examples: Example[] = [
120120
__dirname +
121121
"/../examples/algebraic-data-types/algebraic-data-types.rs",
122122
"utf-8"
123-
)
124-
}
125-
}
123+
),
124+
},
125+
},
126+
{
127+
key: "async-await",
128+
title: "Concurrency with async/await",
129+
code: {
130+
swift: readFileSync(
131+
__dirname +
132+
"/../examples/async-await/async-await.swift",
133+
"utf-8"
134+
),
135+
kotlin: readFileSync(
136+
__dirname +
137+
"/../examples/async-await/async-await.kt",
138+
"utf-8"
139+
),
140+
typescript: readFileSync(
141+
__dirname +
142+
"/../examples/async-await/async-await.ts",
143+
"utf-8"
144+
),
145+
rust: readFileSync(
146+
__dirname +
147+
"/../examples/async-await/async-await.rs",
148+
"utf-8"
149+
),
150+
},
151+
},
126152
];

0 commit comments

Comments
 (0)
Please sign in to comment.