abstract-state-router icon indicating copy to clipboard operation
abstract-state-router copied to clipboard

Non-async resolve results in confusing behavior

Open ArtskydJ opened this issue 3 years ago • 1 comments
trafficstars

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.

ArtskydJ avatar Jul 05 '22 20:07 ArtskydJ

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.

TehShrike avatar Jul 05 '22 21:07 TehShrike