1

As I learned by doing: NextJS Server Actions are synchronous.

I want to achieve concurrency; and I like the syntax of having this abstracted; therefore I've created a NextJS API endpoint that has the following syntax:

import { handleError } form '@/lib/utils';
import { z } from 'zod';

const schema = z.object({ .. }); // some random unrelated zod schema

export async function POST(req: Request) {
  try {
    const myArgs = schema.parse(await req.json());

    const data = await myServerAction(myArgs);

    return Response.json({ data }, { status: 200 });
  } catch (error) {
    return handleError(error, req);
  }

However, I'm not entirely positive this runs concurrently.

Is there anybody that can confidently say/prove that this is indeed concurrent and not synchronous as the Server Actions are?

2

1 Answer 1

0

Even though there is no documentation on this anywhere, I have tested this many times and the answer is here

Network tab of multiple requests

Code:

// api/relevant-chapters/route.ts
import { myServerAction } from '../actions.ts';
export async function GET(req: Request) {
  const result = await myServerAction();
  return Response.json({data: result});
}

// actions.ts
'use server';
export const myServerAction = async () => {
  return await someAsyncQuery();
};

Conclusion: Yes this runs concurrently when you import server actions in API route handlers.

Not the answer you're looking for? Browse other questions tagged or ask your own question.