Code Lab

Modo entrevista. Cronômetro de 5 min, sem dicas e sem autocompletar

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.

Exemplos

useResource([], () => 42)

Retorna 42

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

Retorna [ 'open', 'close' ]

Fonte

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

Os testes ocultos rodam no envio