All Challenges
1 PointBasic
00:00

MERN Stack: React useAsync & useDebounce Hook with AbortController Cancellation

Implement a production-grade React async state manager with AbortController signal cleanup and stale response prevention.

### Problem Overview In MERN stack single-page applications, handling asynchronous API requests inside React components often causes memory leaks and race conditions when fast user interactions trigger overlapping requests. You must build an Async Operation Hook Engine that manages async lifecycle states while providing automatic request cancellation via AbortController. ### Functional Requirements 1. **`execute(asyncFn: (signal: AbortSignal) => Promise<T>): Promise<T | null>`**: - Triggers the async operation, transitions state to `loading: true, error: null`. - Passes an active `AbortSignal` to the callback. - If a previous operation is pending, automatically calls `abort()` on the previous controller. 2. **State Representation**: - `data: T | null`, `error: Error | null`, `isLoading: boolean`, `isSuccess: boolean`, `isError: boolean`. 3. **`cancel(): void`**: - Manually aborts the current execution and resets loading state. 4. **`reset(): void`**: - Resets state back to initial idle state.
AI Review Rubric Dimensions (1–10 Points):
  • AbortController lifecycle
  • Race condition elimination
  • AbortError suppression
  • State machine transitions
solution.tsTYPESCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
27 lines•565 characters•
AI Review Ready