Code Lab

Modo entrevista. Temporizador de 5 min, sin pistas y sin autocompletado

Close in finally

FácilErrors5 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.

Ejemplos

useResource([], () => 42)

Devuelve 42

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

Devuelve [ 'open', 'close' ]

Fuente

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

Las pruebas ocultas se ejecutan al enviar