Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

flow.rule should provide callback support #94

Closed
bthibault opened this Issue · 4 comments

2 participants

@bthibault

I believe that is the issue here:

 // define the rule
    flow.rule("RULE", criteriaArray,
      function(facts) {
         _self = this;
         // do some fact modification asynchronously 
         ruleAction.run(facts.p, rg.then.params, function(err, results) {
                // alert the engine the rule has been modified
                _self.modify(facts.p); 
                _self.retract(facts.p); 
         });
  });

So whats happening here, is the async function ruleAction.run is run, BUT the rule has already fired, which moves the pointer on the linked list then we try to call _self.modify but the fact has already been unlinked.

So we have an async call, with no way to do a callback.

@bthibault

admittedly i'm a node / js n00b so there could be another for me to structure it.

@doug-martin
Owner

Try this

 // define the rule
    flow.rule("RULE", criteriaArray,
      function(facts, engine, next) {
         _self = this;
         // do some fact modification asynchronously 
         ruleAction.run(facts.p, rg.then.params, function(err, results) {
                if(err){
                    next(err);
                }else{
                // alert the engine the rule has been modified
                _self.modify(facts.p); 
                _self.retract(facts.p); 
                next();
               }
         });
  });
@doug-martin
Owner

I should also note if you are using the DSL then you can just use next. See the action section for a detailed explanation. I'll also update the readme to have a section dedicated about asynchronous actions in nools, since this isn't the first time some one has brought this up.

@bthibault

hey hey. it worked.

Thanks

@doug-martin doug-martin referenced this issue from a commit in doug-martin/nools
@doug-martin doug-martin v0.2.3
* Added new `getFacts` method to allow for querying of facts currently in session. #52;
* Added indexing on comparison operators (i.e. `>, <, >=, <=`).
* Updated documentation.
   * Added new section about retrieving facts from a session.
   * Created new section for async actions to address #94
8dd364c
@doug-martin doug-martin referenced this issue
Merged

v0.2.3 #95

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.