Last active
June 28, 2018 21:41
-
-
Save mbergal/75ceb0fcca9082e03e3b4bb461ee4ea6 to your computer and use it in GitHub Desktop.
Js.Promise-error-handling.re
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exception SomeError; | |
exception JsError(Js.Exn.t); | |
let makeJsError: unit => Js.Promise.t(unit) = [%bs.raw | |
{|() => Promise.reject(new Error("aaaa"))|} | |
]; | |
let catch_ = (ocamlhandler, jshandler, promise) => { | |
let a = e => | |
switch (ocamlhandler(e)) { | |
| Some(x) => x | |
| None => jshandler(e) | |
}; | |
promise |> Js.Promise.catch(error => a(error)); | |
}; | |
Js.Promise.resolve() | |
/* |> Js.Promise.then_(() => Js.Promise.reject(SomeError)) */ | |
|> Js.Promise.then_(() => makeJsError()) | |
|> catch_( | |
[@bs.open] | |
( | |
fun | |
| SomeError => { | |
Js.log("SomeError"); | |
Js.Promise.resolve(); | |
} | |
), | |
[%bs.raw | |
{| | |
function(e) { | |
console.log(">>>>>>>>>>", e); | |
} | |
|} | |
], | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment