HardPro challengeJavaScriptTypeScript
Suspense Resource Cache
ReactSuspenseAsync
Implement createResource(asyncFn) returning { read }:
- On first
read(): throws the pending Promise (Suspense pattern). - On subsequent reads after resolve: returns the value.
- On reject: throws the error.
solve(scenarios) simulates calls. Returns final state for each scenario.
Each scenario: { delay, value, error }. The harness awaits the promise then re-reads.
Sample tests
Test #1Resolves successfully
Input: [[{"value":"data"}]]
Output: [{"ok":true,"value":"data"}]
Test #2Rejects with error
Input: [[{"error":"oops"}]]
Output: [{"ok":false,"error":"oops"}]
Test #3Two resources
Input: [[{"value":"a"},{"value":"b"}]]
Output: [{"ok":true,"value":"a"},{"ok":true,"value":"b"}]
Test #4Mixed resolve/reject
Input: [[{"value":"x"},{"error":"fail"}]]
Output: [{"ok":true,"value":"x"},{"ok":false,"error":"fail"}]
Test #5No scenarios
Input: [[]]
Output: []