Create a gist now

Instantly share code, notes, and snippets.

@xtuc /test.re Secret
Last active Oct 30, 2017

What would you like to do?
type either 'l 'r = ('l, 'r);
[%%bs.raw {|
function success(v) { return [v, ""] };
function failwith(e) { return ["", e] };
|}];
let testJsThrows: string => either string string = [%bs.raw
{|
(function (x) {
try {
throw "Error";
return success(x);
} catch (e) {
return failwith(e);
}
})
|}
];
let testJsSucceed: string => either string string = [%bs.raw
{|
(function (x) {
try {
return success(x);
} catch (e) {
return failwith(e);
}
})
|}
];
type eval =
| Success string
| Error string;
let runtime_to_either v =>
switch v {
| (l, "") => Success l
| ("", r) => Error r
| (_, _) => failwith "Could not parse JavaScript output"
};
switch (testJsThrows "test" |> runtime_to_either) {
| Success x => Js.log2 "testJsThrows" x
| Error e => Js.log2 "testJsThrows" e
};
switch (testJsSucceed "test" |> runtime_to_either) {
| Success x => Js.log2 "testJsSucceed" x
| Error e => Js.log2 "testJsSucceed" e
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment