正确的异步调度模式

时间:2017-11-17 14:55:01

标签: redux redux-thunk

请帮助我,是否使用redux thunk进行异步调度是正确的模式?

//actions types
const ADD_ARTIST = 'ADD_ARTIST'

// action
function AddArtist(val){
  return {
    type: ADD_ARTIST,
    payload:val
  }
}


//some async events 
function async1(id){
  return fetch(.....)
}

function async2(id){
  return fetch(.....)
}

function async3(id){
  return fetch(.....)
}

function auth(id) {
  return function (dispatch) {
    async1(10).then( (v) =>  dispatch(AddArtist(v)) );
    Promise.all([
      async2(26),
      async3(51),
    ]).then(
          (v) => dispatch(AddArtist(v[0] + v[1] )
      ));
  }
}


var ViewWrapper = connect(
  state => ({
    playlist: state.playlist,
    artist: state.artist,
  }),
  dispatch => (
    {
      dispatch,
      auth: bindActionCreators(auth, dispatch)
  })

)(View);

和按钮:

  <input type='button' onClick={() => 
this.props.dispatch( this.props.auth() ) }   value='dispatch mapDispatch>>>>>>>' />

1 个答案:

答案 0 :(得分:1)

根据redux documentation,这看起来不错,但您也会将一些代码应用于错误处理。

在这里你可以写出更好的方式

this.model.stage.focusedProperty().addListener((ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) -> {
        if (!mainModel.blockFocusRequest){
        if (t1){
            try{
                mainModel.openWindows.remove(this.model.stage);
                mainModel.openWindows.add(0,this.model.stage);
            mainModel.blockFocusRequest = true;
            for (int i=mainModel.openWindows.size()-1;i>=0;i--){
                mainModel.openWindows.get(i).toFront();
            }                              
            } finally{
                mainModel.blockFocusRequest = false;
            }
        }
        }
    });