defer
Run an async function with deferred functions
Usage
The _.defer
functions lets you run an async function, registering functions as you go
that should be deferred until the async function completes, and then executed. This is
really useful in scripts where failure up to or after a specific point will require some
cleanup. It’s a bit like a finally
block.
A hat tip to Swift’s defer
for the inspiration.
The function passed to _.defer
is called with a single register
function argument that
can be used to register the work you want to be called when the function completes. If your function throws an error and then a registered cleanup function throws
and error it is ignored by default. The register
function supports an optional second options
argument that lets you configure a rethrow
strategy so that error in the cleanup function is rethrown.