Course contentsShow
← React
Lesson 595 of 2,39616. useEffect for Data FetchingFree lesson

AbortController with POST/PUT Requests

Applying the same pattern to mutations, though canceling writes has different semantics.

AbortController with POST/PUT Requests

What you'll learn: How to apply AbortController to mutation requests (POST/PUT), though canceling writes has different implications than canceling reads.

The Technical Pattern Still Works

You can absolutely use AbortController with POST or PUT requests the same way you do with GET:

useEffect(() => {
  const controller = new AbortController();

  async function createItem() {
    try {
      const response = await fetch('/api/items', {
        method: 'POST',
        signal: controller.signal,
        body: JSON.stringify({ name: 'New Item' }),
        headers: { 'Content-Type': 'application/json' }
      });
      // handle response...
    } catch (error) {
      if (error.name === 'AbortError') {
        console.log('POST was canceled');
      }
    }
  }

  createItem();
  return () => controller.abort();
}, [someDependency]);

The Semantic Difference

Here's the critical thinking shift: canceling a write request doesn't undo what was written.

If your POST request was aborted mid-flight, the server might have already processed it. Unlike fetching data (where abandoned reads are harmless), an abandoned POST could mean:

  • The database row was created, but you never got confirmation
  • Payment was processed, but the component unmounted
  • The server is still executing the mutation even though your UI moved on

When to Use Abort with Mutations

Abort is most appropriate for mutations when:

  • The operation is idempotent (safe to repeat)
  • You're preventing accidental duplicate submissions
  • The server supports transaction rollback for incomplete requests

For critical operations (payments, data deletion), you typically need server-side idempotency keys or transaction IDs instead of relying solely on client-side abort.

Key Takeaway: AbortController works syntactically with POST/PUT, but canceling a write doesn't guarantee the write didn't happen—understand your server's behavior and use abort primarily for preventing UI inconsistency, not data inconsistency.