is there a way to pass a suspend function instead ...
# javascript
a
is there a way to pass a suspend function instead of a regular function into an external object's function parameters? I am using express js from kotlinjs like this
Copy code
external object Express {
    fun get(path: String, handler: (req: dynamic, res: dynamic) -> Unit)
}
And I would like to do
Copy code
express.get("/") {
    // suspend here ?
}
similar to how in normal js i would just add async into the function like this:
Copy code
app.get('/', async (req, res) => {
    // I can use await here
})
I tried just marking the handler as a
suspend
function but express throws saying hander is not a function
j
the function returns a promise so instead of
{ suspend code here }
do
{ GlobalScope.async { suspend code here } }
you might have to
asPromise()
that. been a few years since i've done anything with promises
a
my bad. the return type is wrong while experimenting with something. it should in fact be -> Unit. (i edited my snippet) my question is specific to how do I pass async functions similar to how i would do in js I already launch my own coroutine as a workaround from within the handler. would love to know if this is supported by kotlin/js instead
j
No
If it's a regular function you only get to run synchronous code or schedule on the event loop
👍 1
t
If
async
handler supported by
express
- then signature is invalid and it must be
Promise<Void>?
instead of
Unit
a
@turansky it doesn't. the real signature in js is:
Copy code
export type RequestParamHandler = (req: Request, res: Response, next: NextFunction, value: any, name: string) => any;
in normal js i use
async
so that i get to use
await
inside the callback, but express doesnt really use the promise
t
As I see - it supports async handlers
a
i'm not interested in changing frameworks. i can figure out the syntax i want (ie adding some extension function). my question was about whether it is possible or not, and it seems like it is not
t
Do you write declarations for
express
yourself?
a
yup