frank
06/03/2020, 12:57 PMasync
and await
from middleware of koa.
Koa router:
const router = require('koa-router')()
router.get('/', async (ctx, next) => {
await ctx.render('index', {
title: 'Hello Koa 2!'
})
})
I tried with js() function and coroutines but cant make it work.
Any hint or confirm if avaible in kotlin/JS?araqnid
06/03/2020, 1:53 PMaraqnid
06/03/2020, 1:54 PMpromise { … }
is effectively an async function, and you can call .await()
on a promise expressionaraqnid
06/03/2020, 1:55 PMaraqnid
06/03/2020, 1:56 PMrouter.get("/", { ctx, next -> promise {
ctx.render("index", jsObject {
title = "Hello Koa 2!"
}).await()
} })
frank
06/03/2020, 2:13 PMpromise{}
block inside of router? I try with coroutineScope
but throws msg error Suspension functions can be called only within coroutine body
external fun require(module: String): dynamic
suspend fun foo() {
val ejsArgs: dynamic = object {}
ejsArgs["title"] = "Hello Koa 2!"
val router = require("koa-router")()
router.get("/") { ctx, next ->
coroutineScope {
promise {
ctx.render("index", ejsArgs).await()
}
}
}
return router
}
araqnid
06/03/2020, 2:18 PMfrank
06/03/2020, 2:37 PMrouter.get("/index2") { ctx, next ->
GlobalScope.promise {
//js("await") Error in when call in browser
ctx.render("index", ejsArgs)
}
}
But dont have await
.Evan R.
06/03/2020, 2:42 PMctx.render("…").await()
. The body of GlobalScope.promise{ }
is a suspending lambda so you should be able to invoke it there.frank
06/03/2020, 2:44 PMctx.render("index", ejsArgs).await()
throws error when call middleware:
server error TypeError: this.local$closure$ctx.render(...).await is not a function
at Coroutine$foo$lambda$lambda.doResume (..\routes\node\index2.js:32:87)
Do you know if this might break in future or kotlin/JS implicitly add await
?frank
06/03/2020, 2:47 PMawait()
work but if I add await
throws error in middleware call.Evan R.
06/03/2020, 2:55 PMimport kotlin.js.Promise.await
Evan R.
06/03/2020, 2:57 PMfrank
06/03/2020, 3:10 PMctx.render()
is variable of Koa.js and it will never have await()
function of coroutines.
¿I need add await() in promise or another coroutine class?Evan R.
06/03/2020, 3:11 PMPromise.await()
extension function on it. That’s what extension functions do — they can be called on a type without creating a new subclassEvan R.
06/03/2020, 3:13 PMrouter.get("/index2") { ctx, next ->
GlobalScope.promise {
val renderPromise: Promise<Any> = ctx.render("index", ejsArgs)
renderPromise.await()
}
}
frank
06/03/2020, 3:26 PMvar await_0 = $module$kotlinx_coroutines_core.kotlinx.coroutines.await_t11jrl$;
function(){
............
var renderPromise = this.local$closure$ctx.render('index', this.local$closure$ejsArgs);
this.state_0 = 2;
this.result_0 = await_0(renderPromise, this);
.............
}
I hope it does not break in the future.frank
06/03/2020, 3:33 PMEvan R.
06/03/2020, 3:43 PMGlobalScope.launch {}
or runBlocking {}
just like you can on the JVM
By coroutine bridges, I mean these extension functions:
• https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/kotlin.js.-promise/index.html
• https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/promise.htmlfrank
06/03/2020, 3:49 PMaraqnid
06/03/2020, 6:45 PMrunBlocking
in JS, because there’s no blocking in JSEvan R.
06/03/2020, 10:10 PM