How to cancel a server action? #54516
Replies: 8 comments 6 replies
-
As far as I know, when the actions are sent to the server, you can't undo them or affect them from outside. |
Beta Was this translation helpful? Give feedback.
-
Any updates on this? |
Beta Was this translation helpful? Give feedback.
-
This would be useful I think. |
Beta Was this translation helpful? Give feedback.
-
This would be useful especially with strict mode when useEffect runs twice in a row. |
Beta Was this translation helpful? Give feedback.
-
waiting for updates on this as well |
Beta Was this translation helpful? Give feedback.
-
we need can cancel server actions! |
Beta Was this translation helpful? Give feedback.
-
I am currently on a similar issue. Earlier with fetch('/api/...') and the like I was creating an AbortController in the mount effect, then doing the fetch with the related abortSignal and returning a destructor from the effect which aborts the abortController and so the fetch. Now, with server actions, I will do the following:
Simple example:
|
Beta Was this translation helpful? Give feedback.
-
Hey, is there any issue on this because i created a sandbox example but and tried some work around but didn't work at all, could you please suggest me what's the work around if you get the solution... sandbox URL: sandbox I am using nextjs - 15, with react 19 'use server';
import { setTimeout } from 'timers/promises';
type ActionId = number;
interface ActionResponse {
success: boolean;
error?: string;
}
const activeActions: Map<ActionId, boolean> = new Map();
export async function handleAction(data: Record<string, unknown>, id: ActionId): Promise<ActionResponse> {
activeActions.set(id, true);
try {
for (let i = 0; i < 10; i++) {
if (!activeActions.get(id)) {
throw new Error('Action aborted');
}
console.log(`Processing step ${i + 1}`);
await setTimeout(1000); // Simulated delay
}
return { success: true };
} catch (error) {
return { success: false, error: error instanceof Error ? error.message : 'Unknown error' };
} finally {
activeActions.delete(id);
}
}
export async function abortAction(id: ActionId) {
activeActions.set(id, false);
}
and the component is "use client";
import { useTransition, useState } from "react";
import { handleAction, abortAction } from "../server";
type ActionId = number;
interface ActionResponse {
success: boolean;
error?: string;
}
export default function MyComponent() {
const [isPending, startTransition] = useTransition();
const [actionId, setActionId] = useState<ActionId | null>(null);
function startAction(): void {
const id: ActionId = Date.now();
setActionId(id);
startTransition(() => {
handleAction({ key: "value" }, id)
.then((res: ActionResponse) => {
if (res.success) {
console.log("Action completed");
} else {
console.log("Action aborted or failed:", res.error);
}
})
.finally(() => setActionId(null));
});
}
function stopAction(): void {
if (actionId !== null) {
abortAction(actionId);
setActionId(null);
}
}
return (
<div>
<button onClick={startAction} disabled={isPending || actionId !== null}>
Start Action
</button>
<button onClick={stopAction} disabled={actionId === null}>
Abort Action
</button>
</div>
);
}
|
Beta Was this translation helpful? Give feedback.
-
Summary
I am ramping up on server actions and I was trying to understand if there is a way to cancel them?
Let's say have an action that can take several seconds to run and you want to give the opportunity to the user to cancel the action.
With
fetch
you could useAbortController
to do that, but I couldn't find anything on that topic regarding server actions.Does anyone know?
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions