ktorio / ktorio/ktor

Make ApplicationCall.respondHtml and ApplicationCall.respondHtmlTemplate accept suspendable functions

Open
#976 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
14.5k
Forks
1.3k
Avg merge
2d 13h
Merged PRs (30d)
49

Description

### Ktor Version
1.1.3

### Ktor Engine Used(client or server and name)
ktor-server-servlet

### JVM Version, Operating System and Relevant Context
1.8, macOS

### Feedback
I am working on an application that uses `ktor-html-builder` combined with asynchronous API calls, and I am having a hard time getting clean and concise code because I am not allowed to `.await()` within my HTML blocks.

Right now, I am forced to do this:

``` kotlin
Routing.routes() {
get("/") {
val firstResultDeferred = async { firstCall() }
val secondResultDeferred = async { secondCall() }

val firstResult = firstResultDeferred.await()
val secondResult = secondResultDeferred.await()

call.respondHtml {
body {
p {
+ "First result: $firstResult"
}
p {
+ "Second result: $secondResult"
}
}
}
}
}
```

Instead, I would like to be able to do this:

``` kotlin
Routing.routes() {
get("/") {
val firstResult = async { firstCall() }
val secondResult = async { secondCall() }

call.respondHtml {
body {
p {
+ "First result: ${firstResult.await()}"
}
p {
+ "Second result: ${secondResult.await()}"
}
}
}
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start at the ktor-html-builder implementations of ApplicationCall.respondHtml and ApplicationCall.respondHtmlTemplate, the entry points named in the report. Trace how their HTML-builder lambdas are defined and verify both APIs support await() inside the blocks while preserving the shown HTML output.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.