Code Lab

Close in finally

EasyErrors5 min

Write a function useResource(log, work). First it adds the string 'open' to the array log, then it calls work() and returns its result. At the end 'close' always goes into log, even when work() throws. Do not swallow that error, let it go on as it is.

Examples

useResource([], () => 42)

Returns 42

(() => { const log = []; useResource(log, () => 'done'); return log; })()

Returns [ 'open', 'close' ]

Source

Loading the editor...
  1. Returns the result of work, log has open and close
  2. The error from work goes on, and close is in log

Hidden tests run on Submit.