close

async-server-action

Added in v0.9.4

Configuration

rslint.config.ts
import { defineConfig, reactPlugin } from '@rslint/core';

export default defineConfig([
  reactPlugin.configs.recommended,
  {
    rules: {
      'react/async-server-action': 'error',
    },
  },
]);

Rule Details

Require functions whose first statement is "use server" to be async.

React Server Actions must be async even when they do not await anything or return a promise. This rule checks function declarations, function expressions, arrow functions, and methods.

Examples of incorrect code for this rule:

function action() {
  'use server';
  save();
}

<form
  action={() => {
    'use server';
    save();
  }}
>
  Save
</form>;

Examples of correct code for this rule:

async function action() {
  'use server';
  await save();
}

<form
  action={async () => {
    'use server';
    await save();
  }}
>
  Save
</form>;

Only the first statement is checked. A preceding statement, including another directive such as "use strict", prevents a match. Generators and template literals are ignored. A file-level "use server" directive does not cause this rule to check all exported functions.

The rule offers an editor suggestion to insert async; it does not apply an automatic fix.

Options

This rule has no options.

Differences from upstream

  • For computed methods, rslint suggests async [action]() { 'use server'; }. Upstream inserts async inside the brackets, which can produce invalid syntax or change the method name.
  • Constructors, getters, and setters are reported without suggestions because they cannot be async. Upstream offers edits that can produce invalid syntax or rename an accessor. Move the Server Action into an ordinary async function or method instead.

When Not To Use It

If you are not using React Server Components.

Original Documentation

This rule follows the upstream commit linked below; it is not included in upstream release v7.37.5.