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

Async/await pattern with async actions #536

Open
rkpatel33 opened this issue Oct 27, 2017 · 3 comments
Open

Async/await pattern with async actions #536

rkpatel33 opened this issue Oct 27, 2017 · 3 comments

Comments

@rkpatel33
Copy link

I would like to use asyc/await instead of flux async actions with action.completed/action.failed child actions, but after staring at some of the issues related to async actions, I'm unsure how to proceeds. I've looked at this one in particular:

#524

The core of what I am trying to accomplish is to call two async actions aimed at to different stores, one after the other in a React component as follows:

// Inside some React class component

async componentDidMount() {
    // Aimed at the `ChannelQueueStore`
    await ChannelQueueActions.loadTickets(this.props.loggedInAgent);
    // Aimed at the `ChatStore`
    await ChatActions.updateChannel(this.state.selectedChannel);
  }

My ChannelQueueStore as currently implemented is defined below, and as written the above code does not work. ChatActions.updateChannel is triggered immediately after ChannelQueueActions.loadTickets instead of waiting.

I'm wondering if someone can suggest a way to refactor the below code so the above will work.

// Inside my `ChannelQueueStore` store
export default class ChannelQueueStore extends Reflux.Store {
  constructor() {
    super();
    this.state = {
      selectedChannel: null,
      channels: []
    };
    this.tw = new TwilioWrapper(); // Has a bunch of promise returning async functions
    this.listenTo(ChannelQueueActions.loadTickets, this.onLoadTickets);
  }

  async onLoadTickets(agentID) {
    // getOpenChannelsOwnedbyAgentSerialized is an `async` function, use await to wait until its done.
    let channels = await this.tw.getOpenChannelsOwnedbyAgentSerialized(agentID);

    this.setState({
      selectedChannel: channels[0].uniqueName,
      channels: channels
    });
  }
}

I could call ChatActions.updateChannel(this.state.selectedChannel) from at the end of onLoadTickets, which works but seems like bad form, and I think putting both action calls in the component itself makes it easier to see whats going on in the component.

Thx.

@kra3
Copy link

kra3 commented Nov 9, 2017

What you are trying to achieve is what gaearon/redux-thunk or redux-saga/redux-saga does for redux.
That is chain together asynchronous operations/side-effects.

Reflux way of achieving this is through child actions (with async enabled.)
Have a look at the documentation at https://github.com/reflux/refluxjs/tree/master/docs/actions#asynchronous-loading-via-child-actions

@rkpatel33
Copy link
Author

Got it, thanks for the response!

@songguangyu
Copy link

image
this demo is to easy, can provide a complete demo ?

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