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

Swap promises for ES6 generators and ES7 async await #37

Closed
tomatau opened this issue Feb 25, 2015 · 11 comments
Closed

Swap promises for ES6 generators and ES7 async await #37

tomatau opened this issue Feb 25, 2015 · 11 comments

Comments

@tomatau
Copy link
Contributor

tomatau commented Feb 25, 2015

I feel this would greatly improve the API usage and also improve error handling - making the code much clear to avoid things like the dirty reThrow function that lives inside Action.

This would require using the babel compiler (6to5) which also supports JSX if mcfly ever goes the route of involving any JSX.

e.g. Currently:

let MyActions = mcfly.createActions({
  actionCreator(foo){
    let scopedVar;
    return asyncCall.method(foo)
      .then(resolved=>{
        scopedVar = resolved.bar;
        return asyncCall.anotherMethod(resolved.derp);
      })
      .then(resolved2=>({
        actionType: Constants.EVENT,
        scopedVar, resolved2
      }))
  }

Would become, something like this (maybe?):

let MyActions = mcfly.createActions({
  actionCreator(foo){
    let resolved = await asyncCall.method(foo)
    let scopedVar = resolved.bar;
    let resolved2 = await asyncCall.anotherMethod(resolved.derp)
    return {
      actionType: Constants.EVENT,
      scopedVar, resolved2
    }
  }
}

Which reads much nicer imo and is forward thinking.

It would also help with #36

@kenwheeler
Copy link
Owner

That api would require end users to use babel though, correct?

@tomatau
Copy link
Contributor Author

tomatau commented Feb 25, 2015

We would just need to use babel in the build process I think? I'm not sure how well it would play with people using McFly in their browserify/webpack/POJ workflow.

@kenwheeler
Copy link
Owner

What do you think about somehow exposing an async prop on an action creator that will allow users to explicitly dispatch?

@tomatau
Copy link
Contributor Author

tomatau commented Feb 25, 2015

Pseudo code an example?

@kenwheeler
Copy link
Owner

MyActions.getPost.async(function(dispatch){

       dispatch({
            actionType: 'GET_POST_BEGIN',
            loading: true
        });

        $.ajax('/foo/bar/')
        .done(function(data){
             dispatch({
                 actionType: 'GET_POST_SUCCESS',
                 data: data,
                 loading: false
             });
        })
        .fail(function(error){
            dispatch({
                 actionType: 'GET_POST_ERROR',
                 error: error,
                 loading: false
             });
        });

});

@kenwheeler
Copy link
Owner

With the above example, MyActions.getPost(param) would still work, async being a special property that doesn't call dispatch implicitly.

@tomatau
Copy link
Contributor Author

tomatau commented Mar 11, 2015

Have been using async await in action creators anyway and it works nicely

@tomatau tomatau closed this as completed Mar 11, 2015
@kenwheeler
Copy link
Owner

Can I see how you're doing that?

@tomatau
Copy link
Contributor Author

tomatau commented Mar 11, 2015

Just compiling with babel, runtime and experimental set, here's an example I've just poached and simplified from our src:

let ItemActions = flux.createActions({
  async createItem(item){
    try {
      let { id, parentId } = item
        , newItem = await itemGateway.createItem(item)
        , page = await fetchPage(id, parentId, ItemsStore.getPage())
        , selectedItem = await selectItem(newItem);
      return {
        actionType : ItemConstants.CREATE_ITEM,
        item : newItem,
        items : page.items,
        page : page.page,
        selectedItem : selectedItem.item,
      }
    } catch(e) {
      return {
        actionType: AppConstants.NOOP
      }
    }
  },

@kenwheeler
Copy link
Owner

Holy shit, that's phenomenal

@tomatau
Copy link
Contributor Author

tomatau commented Mar 11, 2015

This all works as we wrap action creators in promises :)

Tempted to enable babel playground too.. would allow working with memo functions, and static properties for React 0.13 class propTypes and defaults. Lots of goodies!

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

2 participants