0

I call javascript, because this is the only way to archieve it. then i call my Java/kotlin method "splitIt" from javascript to fill my fields. then i want this result to be returned at the end.
but javascript is run at the end, so the result is empty

   fun test01(): List<String> {

        element.node.runWhenAttached { ui ->
            println(1)

            val x = ui.page.executeJavaScript(

                """setTimeout(
                |   function splitMyHTML () {
                |       ${'$'}0._editor.insertText(${'$'}0._editor.getSelection(true).index,"$CODEWORD");
                |       ${'$'}0.${'$'}server.splitIt();
                |   }
                |   ,0
                |)""".trimMargin(), element
            )

        }

        println(2)

        return listOf(firstPart, secondPart)


    }

    @Suppress("unused") // this is used in javascript
    @ClientCallable
    fun splitIt() {
        firstPart = "test1"
        secondPart = "test2"
        println(20)
    }

output print is 1 2 20 and the list is empty

but i want it to be 1 20 2 and the list to be filled with "test1" and "test2"

Naor Tedgi
  • 5,204
  • 3
  • 21
  • 48
unknown404
  • 115
  • 9
  • I don't know anything about the interaction between Kotlin and JS though in JS you'd typically solve this by either a callback (which is what the `element.node.runWhenAttached` would appear to be - so perhaps you haven't done this correctly); or by not executing the second part until the first returns a promise. But it doesn't look like you are in that position. So work on the `element.node.runWhenAttached` part. – HankCa Jun 20 '19 at 10:14
  • 1
    What you see is as expected. The client and server are different machines. The execution in server side, i.e. when you call executeJavaScript(..) will not stop to wait result. There is client - server round trip that takes some time and hence you get "20" after "1" and "2". – Tatu Lund Jun 20 '19 at 10:27
  • Why would you use `setTimeout`? I kinda feel like this is a duplicate of [this question](https://stackoverflow.com/q/111102)... – Siguza Jun 20 '19 at 10:47
  • @TatuLund yes, i get that. but how can i tell my java/kotlin part to wat for the JS result? – unknown404 Jun 22 '19 at 14:43
  • @unknown404 instead you should refactor your code so that you do not have to wait. For example you could have println(1) and println(2) calls in splitIt() function as first and last. I assume these are just representing something else what you actually do in the app, so it depends. – Tatu Lund Jun 22 '19 at 17:29

0 Answers0