ctrlplusb / easy-peasy

Vegetarian friendly state for React

Home Page:https://easy-peasy.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Thunks can no longer return anything

allpro opened this issue · comments

I just tested upgrading the Easy Peasy v3.2 we've been using, and found a major breaking change that is not documented. At first I thought it was a bug, so I also tested the latest v5.0.3 and found the same thing.

Up to v3.2, returning a promise from a thunk would actually return that promise. However thunks now swallow return values/promises. This is not about Easy Peasy detecting or handling thunk failures. That's internal EP logic. Blocking return values to the external calling code is different. When I call a method, and that method explicitly returns a value, I expect the value to be returned, but thunks now return undefined.

If I upgrade EP, my app will break in a hundred places, with no easy work-around. We rely on api-errors, (rejected promises), passing-through thunks back to the component or service that called it so the error can be 'handled'. Occasionally code calling a thunk may chain a then() statement to fire when the action has completed, so it can close a view or continue with a next-step.

If thunks cannot pass-back promises, I'd have to completely rewrite our data layer because thunks are no longer usable as 'middle-men' between the business logic and request layers. I'd probably stop using thunk-actions completely and move requests into a wrapper around the store. This would be a massive change and add significant complexity to our clean, modular data layer. The only other option would be to pass callbacks into thunks, but I would not use this outdated pattern.

Needless to say, I'd like model actions to work like normal methods again, and return what they return. Is there a chance of restoring this functionality? Or will I need to create work-arounds if I ever want to upgrade EP?

Hi @allpro

Wow, that is definitely a regression. The return behaviour should have been maintained, and I am truly surprised this hasn't been reported before. I will mark this for investigation.

I can't reproduce this on v5.0.3 using the code below.

const model = {
    getEmployee: thunk(async (action) => {
        const employee = await getAuthenticatedUser();

        action.setEmployee(employee);

        return employee;
    }),
}
const getEmployee = useStoreActions(actions => actions.getEmployee);

useEffect(() => {
    getEmployee().then(emp => console.log(emp)).catch(error => console.error('error!'));
}, []);

This code runs exactly how I expect it would and the promise chain works fine?

Agreed, there may have been a temporary regression, but recent versions appear to work as expected. 🙏

@DaneEveritt - curious if upgrading fixed this for you?

We're running into a similar situation over on one of our codebases, making an upgrade pass from 3 -> 6. In one codebase, using 6, things return as expected, so all good there. However, on another, nothing returns as you've described. I'm thinking it has something to do with our setup code, but having trouble tracking down the why. Any clues would be appreciated.

Update! Realized that it was due to some outdated custom middleware that we wrote. Fixing that, fixed the chain.