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

Exception in callback #15

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions tests/TestFuture.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ exception TestError(string);
type timeoutId;
[@bs.val] [@bs.val] external setTimeout : ([@bs.uncurry] (unit => unit), int) => timeoutId = "";

describe("Future", () => {
let delay = (ms, f) => Future.make(resolve =>
setTimeout(() => f() |> resolve, ms) |> ignore
);

let delay = (ms, f) => Future.make(resolve =>
setTimeout(() => f() |> resolve, ms) |> ignore
);
describe("Future", () => {

test("sync chaining", () => {
Future.value("one")
Expand Down Expand Up @@ -110,6 +110,37 @@ describe("Future Belt.Result", () => {
| Error(e) => e |. equals("err2");
});
});
/*
test("mapOk (sync exception)", () => {
Belt.Result.Ok("ignored")
|. Future.value
|. Future.mapOk(_ => raise(TestError("boom, goes the dynamite!")))
|. Future.get(r => switch (r) {
| Ok(_) => raise(TestError("shouldn't be possible"))
| Error(TestError(s)) => s |. equals("boom, goes the dynamite!")
| Error(e) => raise(TestError(Js.String.make @@ e))
})
});
*/
/*
testAsync("mapOk (async exception)", done_ => {
delay(25, () => Belt.Result.Ok("ignored"))
|. Future.tap(_ => Js.log("mapOk-async-exception-1"))
|. Future.mapOk(_ => raise(TestError("boom, goes the dynamite!")))
|. Future.tap(_ => Js.log("mapOk-async-exception-2"))
|. Future.get(r => {
Js.log("mapOk-async-exception-3");
switch (r) {
| Ok(_) => raise(TestError("shouldn't be possible"));
| Error(TestError(s)) =>
Js.log("mapOk-async-exception-4");
s |. equals("boom, goes the dynamite!");
done_();
| Error(e) => raise(TestError(Js.String.make @@ e));
}
})
});
*/

test("mapError", () => {
Belt.Result.Ok("three")
Expand Down
24 changes: 23 additions & 1 deletion tests/TestFutureJs.re
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,27 @@ describe("FutureJs", () => {

done_();
});
})
});

testAsync("fromPromise (exception in callback)", done_ => {

let expected = "TestFutureJs.TestError,2,confused!";
let nodeFn = (callback) => callback(TestError("confused!"));
let promise = Js.Promise.make((~resolve as _, ~reject) => {
nodeFn((err) => reject(. err));
});
let future = () => FutureJs.fromPromise(promise, errorTransformer);

Future.value(Belt.Result.Ok("ignored"))
|. Future.tap(_ => Js.log("huh? - 1"))
|. Future.mapOk(_ => raise(TestError("oh the noes!")))
|. Future.tap(_ => Js.log("huh? - 2"))
|. Future.get(r => {
switch(r) {
| Belt.Result.Ok(_) => raise(TestError("shouldn't be possible"))
| Belt.Result.Error((s)) => s |. equals(expected)
};
done_();
});
});
});