Golang Defer implement in Javascript
The behavior of defer statements is straightforward and predictable. There are three simple rules:
- A deferred function’s arguments are evaluated when the defer statement is evaluated.
√, although you must manually pass arguments. - Deferred function calls are executed in Last In First Out order after the surrounding function returns.
√, just reverse the stack! - Deferred functions may read and assign to the returning function’s named return values.
√, no if return a value, yes return an object. Support async function.
godefer(defer=>{
...somecode
defer(somefunc)
...somecode
return 42 //return an object for named return values
})
see more usage in test.js
- Variables declared by let/const can't be use in finally block, have to use var.
- Deferred functions will not run if encounter errors before, but finally block don't know.