Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to exit a Step chain before finishing all registered steps #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

diogogmt
Copy link

I started to convert all the asynchronous calls of a project I'm currently working on to use Step.
Everything worked perfectly. However we had some problems in the following use case:

We need to perform three asynchronous calls to a database before we did some calculations.

Sometimes, during the second async call, an error would occur and we needed to stop executing and send a message back to the client.

The problem was that if we returned from the function that caught the error, it would go on to execute the next Step call. We wanted to stop the execution completely and send a response back to the client indicating the error.

The proposed patch adds a new function to the |next| object.
The exitChain function clears the array with the registered Step calls, thus stopping the execution whenever exitChain is called.

I also added a unit test to check the new feature.

@cybersam
Copy link

I don't see why any new support needs to be added. You should already be able to exit from the middle of a step chain.

All your step functions (except the first and last ones) should follow this pattern:

function(err, ...) { if (err) throw err; ... };

The last step function should look like:

function(err, ...) { if (err) {/* Handle error in some way and return */}; ... }

In any step function (except the last one), you can just throw an error if you need to exit from the chain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants