I found the try/catch block in question:
It was incredibly frustrating to dig further, given the inconsistent naming, not to mention all the wrapping and unwrapping that gets done.
Anyway, the “reject
” function is defined here, meaning it passes the error to the “cb
” function. Looking at Future.create()
, it turns out that cb
is future.setState()
, so the state will either be set to a value or an error.
But when will that happen? Future.create()
immediately calls this callback function, which immediately calls callback
, but the two cb
calls are wrapped in functions themselves, so they aren’t called immediately. It all depends when callback
calls the arguments it was passed.
As previously mentioned, callback
is this anonymous function, which defers things using the then()
function.
At this point, it’s worth distinguishing between all the Promise
objects floating around.
Promise 0: The one from Promise.create()
, stored in your array.
Promise 1: The one created from your array using Promise.afterAll()
.
Promise 1.1: A dummy Promise
created during afterAll()
. (There’s only one because your array has only one item.)
Promise 2: The one created from Promise 1 using success()
.
As expected, Promise 1 is the one that the then()
function is waiting for.
Now it’s time to figure out what happens when your assets finish loading.
- The loader calls your
onComplete
function.
- Your
onComplete
function calls resolve()
. (resolve()
is an anonymous function that passes its argument to setState()
.)
-
resolve()
passes thx.Nil.nil
to Promise 0’s setState()
function.
- Promise 0 dispatches a complete “event.”
- This event filters through Promise 1’s chain.
- Promise 1 dispatches a complete “event.”
-
This anonymous function is finally called. Since the chain in step 5 was successful, the argument
r
is Right(v)
, where v
is an array of the results.
- Your success listener is called, from within a
try
block.
- Your function calls
start()
.
-
start()
throws an error.
- The error is caught.
- The
catch
block calls reject()
, passing the error.
-
reject()
is this anonymous function, and you can’t override it.
Having done all that work, I can tell that the solution is actually not that hard. However, I cannot in good conscience support this library, and I’d much rather encourage you to switch to a better one.
Even though it stopped being updated a while ago, Promhx has better documentation and code structure, and it’s had more effort put into it overall. Like thx.promise, It catches your errors. Unlike thx.promise, Promhx automatically prints the errors it catches, along with any extra information it can gather.