/ ReasonML

ReasonML / BuckleScript JavaScript Interop

JavaScript interop in ReasonML / BuckleScript looks intimidating in the beginning but after some time you learn to love it.

I'm quite amazed by it. The following ReasonML code:

[@bs.module]
external fetch : string => Js.Promise.t('a) = "isomorphic-unfetch";

fetch(url)
|> Js.Promise.then_(response => Js.Promise.resolve(response##text()))
|> Js.Promise.then_(data => Js.Promise.resolve(Js.log(data)));

produces this JavaScript:

var IsomorphicUnfetch = require("isomorphic-unfetch");

IsomorphicUnfetch(url).then((function (response) {
    return Promise.resolve(response.text());
})).then((function (data) {
    return Promise.resolve((console.log(data), /* () */0));
}));