TehShrike / abstract-state-router

Like ui-router, but without all the Angular. The best way to structure a single-page webapp.
http://tehshrike.github.io/state-router-example
294 stars 26 forks source link

Non-async resolve results in confusing behavior #150

Open ArtskydJ opened 2 years ago

ArtskydJ commented 2 years ago

I put some test data in the resolve, and forgot to set it as an async function. When I try to load a route, I don't get any error message or anything. It just appears to get stuck.

resolve: () => {
    const thing = {
        test_data: true
    }
    return thing
}

I would have expected it to either work as-is, or to throw an error about a missing .then method.

TehShrike commented 2 years ago

So ASR was originally written in the error-first callback style, and didn't support promises. Promise support was added later.

To maintain backwards compatibility, the callback style is still supported. The way that works is that if the resolve function returns a thenable, ASR internally resolves with the result of that thenable. Otherwise, it expects that the user plans to call the callback function in the future.

https://github.com/TehShrike/abstract-state-router/blob/848dab9cd84bef6d79053e3138c6f39d85c2ecb8/index.js#L413-L424

At this point I'm fine with publishing the breaking change that removes support for callback functions and only exposes the promise API. Callbacks have been dead for a long time.