DEV Community

Cover image for Building Reliable Workflows with Serverless JavaScript
Jbee - codehooks.io
Jbee - codehooks.io

Posted on • Edited on

Building Reliable Workflows with Serverless JavaScript

Modern web apps often need more than just simple API endpoints. You need logic. Not just any logic, but stateful, resilient, and scalable logic β€” think user onboarding flows, approval steps, reminders, and retries. Enter: Workflows.

And now, with Codehooks.io, building these workflows is easy, fast, and JavaScript-native. Let's dive in.

🧠 Why Workflows?

Whether you’re working on a side project or building enterprise-grade applications, workflows help you:

  • Break complex logic into steps
  • Add retries and error handling
  • Pause and resume
  • Scale reliably

πŸ§ͺ A Simple Example: Odd or Even?

You'll need a codehooks.io account and a project before deploying your workflow code.

In this short tutorial we'll implement a simple workflow for deciding if a number is odd or even.

Let’s define a workflow in index.js:

import { app } from 'codehooks-js'

const workflow = app.createWorkflow('parityCheck', 'Check if a number is even or odd', {
  begin: async (state, goto) => {
    state.number = Math.floor(Math.random() * 100)
    goto('check', state)
  },
  check: async (state, goto) => {
    const step = (state.number % 2 === 0) ? 'even' : 'odd'
    goto(step, state) // branch
  },
  even: async (state, goto) => {
    console.log(`${state.number} is even`)
    goto('end', state)
  },
  odd: async (state, goto) => {
    console.log(`Number ${state.number} is odd`) 
    goto('end', state)
  },
  end: async (state, goto) => {
    console.log('Workflow finished', state)
    goto(null, state) // null complete the workflow
  }
})

// REST API to start a new workflow instance
app.post('/start', async (req, res) => {
  const result = await workflow.start({"Initial": "state"});
  res.json(result);
});

// export app interface to serverless runtime
export default app.init();
Enter fullscreen mode Exit fullscreen mode

Deploy your workflow with the CLI command coho deploy or use the Codehooks Studio to work directly in the browser.

Calling the API endpoint POST https:{PROJECT_URL}/start will print the output from the workflow to the log:

dev 2025-06-15T07:03:05.714Z  Number 29 is odd
dev 2025-06-15T07:03:05.913Z  Workflow finished {
  "Initial": "state",
  "nextStep": "end",
  "createdAt": "2025-06-15T07:02:59.682Z",
  "workflowName": "parityCheck",
  "stepCount": {
    "begin": {
      "visits": 1,
      "startTime": "2025-06-15T07:03:02.738Z",
      "totalTime": 6,
      "finishTime": "2025-06-15T07:03:02.744Z"
    },
    "check": {
      "visits": 1,
      "startTime": "2025-06-15T07:03:05.006Z",
      "totalTime": 3,
      "finishTime": "2025-06-15T07:03:05.009Z"
    },
    "odd": {
      "visits": 1,
      "startTime": "2025-06-15T07:03:05.709Z",
      "totalTime": 3,
      "finishTime": "2025-06-15T07:03:05.712Z"
    },
    "end": {
      "visits": 1,
      "startTime": "2025-06-15T07:03:05.907Z",
      "totalTime": 0
    }
  },
  "_id": "684e7023878a78b8ac8338ab",
  "updatedAt": "2025-06-15T07:03:05.907Z",
  "number": 29,
  "previousStep": "odd",
  "instanceId": "684e7023878a78b8ac8338ab"
}
dev 2025-06-15T07:03:05.917Z  Workflow parityCheck 684e7023878a78b8ac8338ab is completed in time: 6.225s πŸŽ‰
Enter fullscreen mode Exit fullscreen mode

πŸ›  How It Works

  • Each step is an async function.
  • goto(step, state) passes updated state and moves to the next step.
  • State is persisted between steps.
  • Calling goto(null, state) finish the workflow instance.

πŸ”„ Real-World Use Cases

  • User onboarding
  • Approval flows
  • Reminders & timeouts
  • Background jobs

🧠 Code-first = AI-ready

One underrated benefit of using a code-first workflow engine like Codehooks? It plays beautifully with AI tools like ChatGPT, Cursor and Copilot.

Because workflows are written in plain, readable JavaScript:

  • βœ… You can ask an LLM to generate your workflows from natural language specs
  • 🐞 You can debug or optimize a step-by-step flow by pasting the code into ChatGPT
  • πŸ” You can analyze edge cases and verify paths programmatically or through simulations

This makes your workflows not just easier to write β€” but easier to evolve, maintain, and reason about, even with AI assistance.

πŸ‘‰ Try it at codehooks.io

Top comments (0)