workflow-for-js
v0.1.0
Published
Provides a workflow for JavaScript
Downloads
64
Readme
workflow-for-js
A JavaScript workflow engine with retry capabilities.
What is a workflow?
A workflow is a system for managing repetitive processes and tasks which occur in a particular order. They are the mechanism by which people and enterprises accomplish their work, whether manufacturing a product, providing a service, processing information or any other value-generating activity.
Install
To install workflow-for-js, you can use NPM:
npm install --save @kevboutin/workflow-for-js
Example
Please reference the example located in index.js
.
Why?
This provides a way to handle retrying various tasks in a sequence. Using a workflow improves code readability and reliability. If you have multiple functions to run sequentially or perhaps the return from one function is a required parameter to the next function, a workflow is a perfect solution.
Usage
As you might have already seen from our example, using workflow is very simple and requires just few steps:
- Import
Workflow
.
import { Workflow } from "./workflow.js";
- Write your functions as usual. In our example, we have
uploadImage
,saveUser
andsendVerificationEmail
.
const saveUser = async ({ email, password, imageLink }) => {
// ...
};
- Create your workflow inside of a try+catch block using
Workflow.createWorkflow()
.
try {
// Creating a workflow with a 4 retry limit.
Workflow.createWorkflow(4, (workflow) => {
// ...
});
} catch (error) {
console.error("Error in workflow.", error);
}
- Add each function as a workflow step using
create()
.
workflow.create(async (image) => {
// ...
});
- Optionally add a final workflow step using
finally()
. Note thatfinally()
will not be retried. Usecreate()
if retries are important.
workflow.finally(async ({ email }) => {
// ...
});
- Run your workflow using
run()
.
Workflow.run(image);
NOTE: Steps will be run in the order they are created.
Handling errors
But, what happens when there is an error?
When there is an error, the step will be retried until the retryLimit
is reached. Once the limit is reached, the error will be thrown to your try+catch block for handling.
If you have a finally function, it will always be called prior to terminating the workflow despite errors in prior steps.
Publishing Releases
Use the following command to publish the various packages from this repository. Afterward, use GitHub to generate a new release based on the root package.json version.
npm publish