redux-logic icon indicating copy to clipboard operation
redux-logic copied to clipboard

Promise/observable returns in validate/transform/process hooks

Open bisubus opened this issue 6 years ago • 2 comments

It appears that hooks don't fully benefit from promise/observable returns. The suggestion is to use promise and observable returns and make callback API optional where possible.

The library already relies on hook functions arity, this could be used to change their behaviour based on function parameters.

This validate hook

async validate({ action }, resolve, reject) {
  try {
    await ...;
    resolve(action);
  } catch (err) {
    reject(err);
  }
}

could become

async validate({ action }) {
  await ...;
  return action;
}

validateOptions/transformOptions could possibly be useful with an option similar to dispatchReturn, just in case the behaviour needs to be unambiguous.

And this multi-dispatch process hook

async process({ action }, dispatch, done) {
  dispatch(...);
  await ...;
  dispatch(...);
  done();
}

could become

async process({ action }, dispatch) {
  dispatch(...);
  await ...;
  dispatch(...);
}

Currently single-dispatch mode is deprecated, call done when finished dispatching warning appears in case process function hook has 2 parameters. Additional doneReturn option (defaults to true for multi-dispatch) could possibly be useful for non-ending logics. I believe this approach supplements #93.

bisubus avatar Dec 02 '18 21:12 bisubus

@bisubus Thanks for the suggestions. I like your ideas. I'll give it some thought.

jeffbski avatar Jan 19 '19 21:01 jeffbski

Yeah, let's get rid of done, please. It looks so 2016. :)

thorn0 avatar Aug 07 '19 15:08 thorn0