配置
buildscript {
ext.kotlin_version = '1.2.0'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
group 'com.itheima'
version '0.0.1-SNAPSHOT'
apply plugin: 'kotlin2js'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core-js:0.22.2'
}
task assembleWeb(type: Sync) {
configurations.compile.each { File file ->
from(zipTree(file.absolutePath), {
includeEmptyDirs = false
include { fileTreeElement ->
def path = fileTreeElement.path
path.endsWith(".js") && (path.startsWith("META-INF/resources/") ||
!path.startsWith("META-INF/"))
}
})
}
from compileKotlin2Js.destinationDir
into "${projectDir.parent}/src/main/resources/static"
dependsOn classes
}
assemble.dependsOn assembleWeb
suspend fun get(path: String): String = suspendCoroutine { cont ->
var request = XMLHttpRequest()
request.open("GET", path, true)
request.onreadystatechange = {
if(request.readyState==XMLHttpRequest.DONE){
cont.resume(request.responseText)
}
}
request.send()
}
看似同步的代码,实际异步的逻辑
launch {
val result = get("/frontend.js")
println("result:"+result)
}