MediumPro challengeJavaScriptTypeScript

useAsync Hook

ReactHooksAsync

Implement useAsync(asyncFn) returning the lifecycle states:
'idle' → 'loading' → 'success' | 'error'.

solve(behavior) returns the final { status, data, error }.
behavior: 'success' resolves with 'data!', 'error' rejects with 'oops'.

Sample tests

Test #1Successful resolve
Input: ["success"]
Output: {"data":"data!","error":null,"status":"success"}
Test #2Rejection
Input: ["error"]
Output: {"data":null,"error":"oops","status":"error"}
Test #3Same success
Input: ["success"]
Output: {"data":"data!","error":null,"status":"success"}
Test #4Same error path
Input: ["error"]
Output: {"data":null,"error":"oops","status":"error"}
Test #5Final consistency check
Input: ["success"]
Output: {"data":"data!","error":null,"status":"success"}