diff --git a/src/Promise.res b/src/Promise.res index 5d28a30..254206d 100644 --- a/src/Promise.res +++ b/src/Promise.res @@ -57,3 +57,5 @@ let catch = (promise, callback) => { @bs.scope("Promise") @bs.val external race: array> => t<'a> = "race" + +external done: Js.Promise.t<'a> => unit = "%ignore" diff --git a/src/Promise.resi b/src/Promise.resi index 2a6dbdc..2bd5cfa 100644 --- a/src/Promise.resi +++ b/src/Promise.resi @@ -99,7 +99,7 @@ In case you want to return another promise in your `callback`, consider using \` let catch: (t<'a>, exn => t<'a>) => t<'a> @ocaml.doc(" -`then(promise, callback)` returns a new promise based on the result of `promise`'s value. +`then(promise, callback)` returns a new promise based on the result of `promise`'s value. The `callback` needs to explicitly return a new promise via `resolve`. It is **not allowed** to resolve a nested promise (like `resolve(resolve(1))`). @@ -120,7 +120,7 @@ Promise.resolve(5) external then: (t<'a>, @uncurry ('a => t<'b>)) => t<'b> = "then" @ocaml.doc(" -`thenResolve(promise, callback)` converts an encapsulated value of a promise into another promise wrapped value. +`thenResolve(promise, callback)` converts an encapsulated value of a promise into another promise wrapped value. It is **not allowed** to return a promise within the provided callback (e.g. `thenResolve(value => resolve(value))`). @@ -234,3 +234,8 @@ external all5: ((t<'a>, t<'b>, t<'c>, t<'d>, t<'e>)) => t<('a, 'b, 'c, 'd, 'e)> @bs.scope("Promise") @bs.val external all6: ((t<'a>, t<'b>, t<'c>, t<'d>, t<'e>, t<'f>)) => t<('a, 'b, 'c, 'd, 'e, 'f)> = "all" + +@ocaml.doc(" +`done` is a safe way to ignore a promise. If a value is anything else than a promise, it will raise a type error. +") +external done: Js.Promise.t<'a> => unit = "%ignore" diff --git a/tests/PromiseTest.js b/tests/PromiseTest.js index e68e37c..5378e0a 100644 --- a/tests/PromiseTest.js +++ b/tests/PromiseTest.js @@ -343,8 +343,7 @@ function runTests$3(param) { testRaiseErrorThrow(undefined); thenAfterCatch(undefined); testCatchFinally(undefined); - testResolveFinally(undefined); - + return testResolveFinally(undefined); } var Catching = { diff --git a/tests/PromiseTest.res b/tests/PromiseTest.res index 2831f67..01cfcaf 100644 --- a/tests/PromiseTest.res +++ b/tests/PromiseTest.res @@ -17,7 +17,7 @@ module Creation = { Test.run(__POS_OF__("Should resolve test"), str, equal, "test") resolve() }) - ->ignore + ->done } let runTests = () => { @@ -55,7 +55,7 @@ module ThenChaining = { Test.run(__POS_OF__("Should be 2"), value, equal, 2) resolve() }) - ->ignore + ->done resolve() }) ->catch(e => { @@ -94,7 +94,7 @@ module ThenChaining = { ->thenResolve(num => { num + 1 }) - ->ignore + ->done resolve() }) ->catch(e => { @@ -108,10 +108,10 @@ module ThenChaining = { } let runTests = () => { - testThen()->ignore - testInvalidThen()->ignore - testThenResolve()->ignore - testInvalidThenResolve()->ignore + testThen()->Promise.done + testInvalidThen()->Promise.done + testThenResolve()->Promise.done + testInvalidThenResolve()->Promise.done } } @@ -249,7 +249,7 @@ module Catching = { Test.run(__POS_OF__("finally should have been called"), wasCalled.contents, equal, true) resolve() }) - ->ignore + ->done } let testResolveFinally = () => { @@ -267,16 +267,16 @@ module Catching = { Test.run(__POS_OF__("finally should have been called"), wasCalled.contents, equal, true) resolve() }) - ->ignore + ->done } let runTests = () => { - testExternalPromiseThrow()->ignore - testExnThrow()->ignore - testRaiseErrorThrow()->ignore - thenAfterCatch()->ignore - testCatchFinally()->ignore - testResolveFinally()->ignore + testExternalPromiseThrow()->Promise.done + testExnThrow()->Promise.done + testRaiseErrorThrow()->Promise.done + thenAfterCatch()->Promise.done + testCatchFinally() + testResolveFinally() } }