diff --git a/src/createAction.js b/src/createAction.js index c7b3ada..e1a929d 100644 --- a/src/createAction.js +++ b/src/createAction.js @@ -36,7 +36,7 @@ export default function createAction(definition) { } definition.children = definition.children || []; - if (definition.asyncResult){ + if (definition.asyncResult || definition.asyncCall){ definition.children = definition.children.concat(["completed", "failed"]); } @@ -52,6 +52,14 @@ export default function createAction(definition) { _isAction: true }, PublisherMethods, ActionMethods, definition); + + if(definition.asyncCall){ + context.listen( (argument) => { + definition.asyncCall(argument) + .then(definition.children.completed) + .catch(definition.children.failed); + }); + } var functor = function() { var triggerType = functor.sync ? "trigger" : "triggerAsync"; return functor[triggerType].apply(functor, arguments); diff --git a/test/creatingActions.spec.js b/test/creatingActions.spec.js index b5483a1..c6d0353 100644 --- a/test/creatingActions.spec.js +++ b/test/creatingActions.spec.js @@ -51,6 +51,13 @@ describe('Creating action', function() { }); }); + it("should create action with the parameter asyncCall",function(done){ + let doLogin = () => { + done(); + }; + let actions = Reflux.createActions({login: {asyncCall: doLogin }}); + actions.login(); + }); describe('Reflux.ActionMethods', function() { afterEach(function(){