Incorporating Async_js and Incr_dom

Hello Everyone,

I am currently trying to build a simple web app (front and back end) in Ocaml. Naturally to create a polished and professional looking application I am trying to make the website send and receive HTTP requests, both GET and POST to process data.

I am using Cohttp to handle all of the server side HTTP processing and have had no issues with that so far. On the front end, I have run into no issues, but the back end though I have hit a road block.

I am using incr_dom and async_js to design the web app and to do the communications respectively. The issue I have run into is, simply, async_js returns things wrapped in a Deferred but incr_dom cannot deal with these. In the Action module and apply_action modules the functions all have to return type Model.t but async_js cannot (to the best of my knowledge) return anything that isnt wrapped in a Deferred.

My question is this then, how do I make these two libraries “play nice” if you will? how can I, from inside of apply_action have an HTTP call from async_js and turn the returned deferred string into a non-deferred Model.t (the root of the problem is removing the deferred without bind which requires you to return a b’ Deferred which I do not want). I think I need to force the evaluation of the deferred string but I am unsure how to do this.

I was looking at the documentation and thought that maybe it would be possible to do the actions I need asynchronously and then catch it at the end of execution with an upon statement, then rip the model out of that and construct a new model using any information I need, then firing a new event with that model to satisfy the type checker.

Any and all help is appreciated, I am sorry this post is a little long winded at times.

I assume you mean front end.

apply_action cannot return a deferred, because it must immediately return the new model. However, since it has access to the State.t, you can use that to schedule events (from the schedule callback you get in on_startup). So, for example, you can do something like:

Deferred.upon (get "/api/something") (fun response ->
  schedule (Update_model_with response))