Await without try/catch
Deeply nested try/catch blocks around every await make request handlers hard to read. A common alternative, borrowed from Go, is to have the call return both the error and the value and let the caller check which one is set.
Implement safeAwait(value). It accepts a promise or a plain value and returns a promise that **always fulfils** with a two-element array. If the input fulfils with v, resolve with [null, v]. If it rejects with e, resolve with [e, null]. A non-promise input is treated as an already-fulfilled value. The returned promise must never reject.
What it has to do
- Always fulfil with an array of exactly two elements.
- Use
null— notundefined— for the empty slot. - Accept plain values as well as promises.
- Never reject, whatever the input does.
Your workspace
Ready to check it?
4 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.
AI Crack & Solution Assist
Stuck? Get instant AI hints or break down the optimal solution.
Stuck? The javascript course covers everything this challenge needs.