Back to all reviewers

Cancel aborted async operations

remix-run/react-router
Based on 3 comments
TypeScript

When working with async operations that can be aborted (like HTTP requests), ensure proper cancellation of related promises and deferred operations to prevent memory leaks and unnecessary work. This applies to both operations created after abortion has occurred and those created before abortion.

Concurrency TypeScript

Reviewer Prompt

When working with async operations that can be aborted (like HTTP requests), ensure proper cancellation of related promises and deferred operations to prevent memory leaks and unnecessary work. This applies to both operations created after abortion has occurred and those created before abortion.

For operations created after abortion, check the abort signal before proceeding:

let result = await handler(args);
if (args.request.signal.aborted && isDeferredData(result)) {
  result.cancel();
}

For operations created before abortion, set up abort listeners to handle cancellation:

if (isDeferredData(result)) {
  if (request.signal.aborted) {
    result.cancel();
  } else {
    let deferResult = result;
    request.signal.addEventListener("abort", () => deferResult.cancel());
  }
}

This pattern prevents resource leaks and ensures that aborted requests don’t continue consuming system resources. Always consider both the proactive case (already aborted) and reactive case (abort during execution) when implementing cancellation logic.

3
Comments Analyzed
TypeScript
Primary Language
Concurrency
Category

Source Discussions