I’m writing a type declaration for koa framework, but I don’t know how to declare a funtion which has promise type.The lib code is here:
app.use(function (context, next) {
next().then(() => {
console.log(‘hello’)
})
});
Now in my project, the type code is
type middleware = ?
@send external use: (app, middleware) => unit = “use”
what do I need to write for type middleware, Can anybody help me?
type middleware = unit => Js.Promise.t<unit>
@send external use: (app, middleware) => unit = "use"
More docs here: Async & Promise | ReScript Language Manual
Emmm…,like this
type context = {mutable body: string}
type middleware = (context, unit => Js.Promise.t<unit>) => unit
@send external use: (app, middleware) => unit = "use"
then my rescript is:
app->use((context, next) => {
next()->Js.Promise.then_();
})
But I got err that say:
FAILED: examples/index.cmj
We've found a bug for you!
/home/ubuntu/projects/rescript/rescript-koa/examples/index.res:11:3-8
9 │ // you can use middleware like this
10 │ app->use((context, next) => {
11 │ next()->Js.Promise.then_();
12 │ })
13 │
This has type: Js.Promise.t<unit> (defined as Js_promise.t<unit>)
Somewhere wanted: 'a => Js.Promise.t<'b>
FAILED: cannot make progress due to previous errors.
You’ve added a context
argument to the definition, but when you call the next
function you don’t pass the context
.
Should be something like: next(context)->Js.Promise.then_();
I changed next()->Js.Promise.then_()
to next()->then()
with @ryyppy/rescript-promise
lib, everything is Ok!!!
Thank you for your help! 