question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Async actions on a rule's onSuccess?

See original GitHub issue

Hi, I noticed that promises were not being observed on an onSuccess of rule callback. Seems this way in the code as well. Is there a workaround to this?

Also, what I may be struggling with is the approach to using rules-engines in general and wanting the truthy state of a rule to fire off an action and update the event parameters with the results. Is there a better approach to this?

    {
        conditions: {
            all: [
                {
                    fact: 'result',
                    path: '.parameters.email',
                    operator: 'notEqual',
                    value: ''
                }
            ]
        },
        event: { },
        onSuccess: ((event, almanac) => {     
            return almanac.factValue('result')
                .then((result) => {
                    // Do something async. Rule engine completes run before this action completes.
                })
        })
    }

Thanks, Steve

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
CacheControlcommented, Apr 22, 2017

@daxiang28 Ya, it’d be nice if there was a more elegant solution. If you think of a feature request that would make this easier, please feel free to file it. For now I’ll close this issue as it appears you’ve found a workaround

0reactions
daxiang28commented, Apr 22, 2017

@CacheControl yes that’s pretty much what I’ve rejiggered my code to. The one last piece I was hoping to figure out without being so hacky is mutating the rule event params in the success-events array

// Using lodash
onSuccess: ((event, almanac) => {
    const updatedParams = { message: 'hi' + user };
    return almanac.factValue('success-events')
        .then(successEvents => _.forEach(successEvents, (successEvent) => {
            if (_.isEqual(successEvent, event)) {
                successEvent.params = updatedParams;
            }
        }));
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Asynchronous execution and onsuccess event handler
Because Javascript is strictly single-threaded, asynchronous operations can only run their callbacks once other code has finished running.
Read more >
Understanding the Event Loop, Callbacks, Promises, and ...
An async function can handle a promise called within it using the await operator. await can be used within an async function and...
Read more >
Async Await JavaScript Tutorial – How to Wait for a Function to ...
Failing to provide the async keyword will result in a syntax error when trying to use await inside a regular function.
Read more >
Async Actions | Enact
It allows an action creator to return a function instead of an object (action) and executes that function when it is returned. This...
Read more >
Asynchronous Programming in Java - Baeldung
Since Java 5, the Future interface provides a way to perform asynchronous operations using the FutureTask. We can use the submit method of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found