Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to tackle a JavaScript function that returns a JS Promise? #1073

Closed
jbhoot opened this issue Dec 5, 2020 · 8 comments
Closed

How to tackle a JavaScript function that returns a JS Promise? #1073

jbhoot opened this issue Dec 5, 2020 · 8 comments
Labels

Comments

@jbhoot
Copy link

jbhoot commented Dec 5, 2020

From what I understand, while lwt provides a counterpart to JS Promise, it doesn't have a mechanism to mix those two (like translating js Promise into lwt).

@TyOverby
Copy link
Collaborator

TyOverby commented Dec 5, 2020

Js_of_ocaml doesn't have official support for the promise API. You'll need to use a library like https://github.com/mnxn/promise_jsoo

@jbhoot
Copy link
Author

jbhoot commented Dec 6, 2020

Thank you. Does Promise support seem likely via #944 ?

@jbhoot
Copy link
Author

jbhoot commented Dec 6, 2020

Update: I opted for promise_jsoo, but the following commands (as recommended in the manual):

>  ocamlfind ocamlc -package js_of_ocaml,js_of_ocaml-ppx,promise_jsoo -linkpkg -o bg.byte bg.ml

>  js_of_ocaml bg.byte

fail to include promise_jsoo in the compiled file (mnxn/promise_jsoo#2).

Dune works, however.

Am I missing anything in the above commands?

@TyOverby
Copy link
Collaborator

TyOverby commented Dec 6, 2020

you need to pass the path to indirect_promise.js to the js_of_ocaml invocation.

if i were you, id stick to using Dune though

@jbhoot
Copy link
Author

jbhoot commented Dec 6, 2020

Your suggestion worked!

js_of_ocaml +promise_jsoo/indirect-promise.js bg.byte

compiled bg.js with its dependencies.

dune, even with release profile, produces a js file of 2.2MB. The above command generated an output of 40KB. That’s a drastic difference. Unless there is a way to drastically cut down the output size of dune, I see much value in the latter solution.

@TyOverby
Copy link
Collaborator

TyOverby commented Dec 6, 2020

dune, even with release profile, produces a js file of 2.2MB. The above command generated an output of 40KB

I think that's because Dune includes the "--sourcemap-inline" command in it's invocation of js_of_ocaml. I don't know how you turn that off, but you can check to see what the difference would be by deleting the last line (it should be a comment) in the .js file

@jbhoot
Copy link
Author

jbhoot commented Dec 13, 2020

Sorry I got time to inspect further today. Anyway, quoting myself from ocaml/dune#4030 (comment):

That's not the case. The last line is not a sourcemap comment in bg.bc.js file.

That inspired me to try another command:

ocamlfind ocamlc -g -package js_of_ocaml,js_of_ocaml-ppx,promise_jsoo -linkpkg -o bg.byte bg.ml

js_of_ocaml --source-map-inline +promise_jsoo/indirect_promise.js bg.byte

With --source-map-inline, js_of_ocaml adds a sourcemap comment starting with //# sourceMappingURL=data:application/json;base64,...., and so produces a larger file, but only with a size of 544K (still smaller than dune's 2.2M).

@hhugo hhugo added the question label Sep 30, 2021
@hhugo
Copy link
Member

hhugo commented Jan 11, 2022

The size issue has been resolved by mnxn/promise_jsoo@174d490

The answer to this issue is to use promise_jsoo. I'll create an issue to document this fact and close this issue.

@hhugo hhugo closed this as completed Jan 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants