Skip to content
This repository was archived by the owner on May 17, 2023. It is now read-only.

Commit 1678df4

Browse files
Clarify external interface behavior a bit
1 parent 720e98e commit 1678df4

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

Building Web Applications with React and Kotlin JS/03_A_First_Static_Page.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Type or paste the above code as the contents of your `render` call. If IntelliJ
102102

103103
While writing HTML in Kotlin just for the sake of it is a noble idea, there are actually a lot more benefits to writing your HTML directly inside Kotlin. A big advantage of using this domain specific language is that we can manipulate our website content using language constructs we are already familiar with. Whether it's conditions, loops, collections, or string interpolation, we can expect them to work the same in HTML as they would in Kotlin.
104104

105-
Now, instead of hardcoding the list of videos, let's actually define them as a list of Kotlin objects and display those objects instead. We'll create a simple data class to hold together the attributes of a video called `KotlinVideo` (we can do this in `Main.kt` or a file of our choice). We will also define a corresponding `external interface` – more on that later.
105+
Now, instead of hardcoding the list of videos, let's actually define them as a list of Kotlin objects and display those objects instead. We'll create a simple data class to hold together the attributes of a video called `KotlinVideo` (we can do this in `Main.kt` or a file of our choice). We will also define a corresponding `external interface`, which will prove valuable at the point where we get real data, from a real API.
106106

107107
```kotlin
108108
external interface Video {

Building Web Applications with React and Kotlin JS/08_Using_an_External_REST_API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ import kotlinx.browser.window
5959
import kotlinx.coroutines.*
6060
```
6161

62-
Let's look at what's happening in this *suspending function*. We `fetch` a video from the API given an `id`, wait for it to actually be available, turn it into a JSON, wait again for the completion of that operation, and return it, cast as a `Video` Kotlin object.
62+
Let's look at what's happening in this *suspending function*. We `fetch` a video from the API given an `id`, wait for it to actually be available, turn it into a JSON, wait again for the completion of that operation, and return it – but before, it casts it to the `external interface Video`, which we defined in an earlier chapter. You will see a warning for this unchecked cast – but this is in the nature of using a JavaScript definition like `fetch`: The compiler _can't know for sure_ that what we get back is actually an instance of `Video`. The compiler just needs to trust the developer on this.
6363

6464
A function call like `window.fetch` returns a `Promise` object. We would have to define a callback handler which gets invoked once the `Promise` is *resolved* and a result is available. However, since we are using coroutines in our project, we can `await` those promises. We're writing code that looks sequential but remains non-blocking. Whenever a function like `await()` is called, the method stops its execution (it *suspends*, hence the keyword). It continues execution once the `Promise` can be resolved.
6565

0 commit comments

Comments
 (0)