DEV Community

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

Posted on • Edited on

1 1 1 2 1

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

Build seamlessly, securely, and flexibly with MongoDB Atlas. Try free.

Build seamlessly, securely, and flexibly with MongoDB Atlas. Try free.

MongoDB Atlas lets you build and run modern apps in 125+ regions across AWS, Azure, and Google Cloud. Multi-cloud clusters distribute data seamlessly and auto-failover between providers for high availability and flexibility. Start free!

Learn More

Top comments (0)

Gen AI apps are built with MongoDB Atlas

Gen AI apps are built with MongoDB Atlas

MongoDB Atlas is the developer-friendly database for building, scaling, and running gen AI & LLM apps—no separate vector DB needed. Enjoy native vector search, 115+ regions, and flexible document modeling. Build AI faster, all in one place.

Start Free

👋 Kindness is contagious

Take a moment to explore this thoughtful article, beloved by the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A heartfelt "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay