Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Async actions (async thunk-like) #56

Closed
gioele-santi opened this issue Jan 27, 2021 · 3 comments
Closed

Question: Async actions (async thunk-like) #56

gioele-santi opened this issue Jan 27, 2021 · 3 comments

Comments

@gioele-santi
Copy link

I recently discovered this project and I find it really promising, but checking readme file and example project I could not figure out how to perform an async action like async thunks in redux.

For example if I need to perform a login with a remote service I would need an action with 2 parameters (id, pw) that should trigger a background request and set a loading= true and then upon completion report success or fail using 2 other actions.

I thought Action Plans could be the right way but I am not sure about passing 2 parameters to an action plan.

Maybe I am missing something?
Thank you.

@kamilpowalowski
Copy link
Contributor

Hi, I looked at my code and found an example of Action plan that takes two arguments:

static func purchaseProduct(
        paymentService: PaymentService,
        product: Product
    ) -> ActionPlan<AppState> {
        ActionPlan<AppState> { store -> AnyPublisher<Action, Never> in
            paymentService.purchaseProduct(product)
                .catch { _ in Just<Pro?>(store.state.proState.pro) }
                .map { ProAction.setProStatus($0) }
                .eraseToAnyPublisher()
        }
    }

You can use the .map function to dispatch two different actions as you described.

@gioele-santi
Copy link
Author

Thank you very much! I will try it tomorrow.

@gioele-santi
Copy link
Author

it works perfectly. I think this example could be useful in the readme and that you could close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants