![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@antoniovdlc/defer
Advanced tools
Go-like defer functions in JavaScript.
This package is distributed via npm:
npm install @antoniovdlc/defer
Go provides the very interesting concept of defer
ing functions until the end of a function's execution.
package main
import "fmt"
func main() {
defer fmt.Println("world")
fmt.Println("hello")
}
// hello
// world
Such built-in construct might be very useful in JavaScript for example, where we sometimes need to do some clean-up, and thus could potentially co-locate it with the instanciation.
You can use this library either as an ES module or a CommonJS package:
import { defer, deferrable } from "@antoniovdlc/defer";
- or -
const { defer, deferrable } = require("@antoniovdlc/defer");
defer
takes a function as argument, which will be called at the end of the execution of the caller function, and a caller function.
For sync functions, defer
simply puts the fn
on the call stack using setTimeout(fn, 0)
.
For async functions, it is a bit more tricky, and we keep track of deferred functions within the caller
itself, via the property __$_deferArr
.
Using
defer
in async functions requires them to be wrapper withdeferrable
to work properly.
defer
works out of the box within sync functions. Unfortunately, for async functions, we need to use a wrapper: deferrable
.
This wrapper returns a function that will compute similarly to its argument, but helps keep track of all the deferred functions within it.
defer
red functions are run sequentially, and as such, if one function throws an error, the following defer
red functions won't run.
defer
red functions in a deferrable
will not run if the wrapped function throws an error.
Let's start with a simple translation of the Go code we shared previously:
function main() {
defer(() => console.log("world"), main);
console.log("hello");
}
main(); // hello world
There can be as many defer
calls in a function as you'd like:
function f() {
defer(() => console.log("1"), f);
console.log("2");
defer(() => console.log("3"), f);
console.log("4");
}
f(); // 2 4 1 3
For async functions, we need to use deferrable
:
const f = deferrable(async function fn() {
defer(async() => await new Promise(
resolve => resolve(console.log("1"))
), fn);
await new Promise(resolve => setTimeout(() => resolve(console.log("2"))));
defer(() => console.log("3"), fn);
console.log("4");
});
await f(); // 2 4 1 3
defer
red functions can also be async functions!
Besides getting inspiration from Go's error handling, this package also draws inspiration from the following packages:
Good you ask!
There is probably a bit of selfishness in the belief that maybe I can implement this in a better way, but the main reason is to see if I can implement such a library and maybe add some goodies on top.
This code, even though thoroughly tested with 100% code coverage, has not ran in production yet. As such, either you feel adventurous, or I would invite you to look for more mature libraries providing essentially the same functionalities (I've linked to a few I found myself right above, but there are many more).
Anyway, thanks for stopping by! :)
caller
function to defer
?JavaScript engines used to support a Function.caller
property that would have made it trivial to access the function in which defer
was being called. Unfortunately, that property (amongst a couple of others) was dropped, hence the only feasible way (at least that I can think of) of getting access to the caller was by adding an extra parameter to defer
directly.
This means that defer
only work in non-anonymous functions.
defer
red functions called in last-in-first-out order like in Go?Good question!
I'd say it comes down to personal preference, as I think it is easier to reason about the order of the defer
red functions when they execute in the same order they appear in the code.
MIT
FAQs
Go-like defer functions in JavaScript.
The npm package @antoniovdlc/defer receives a total of 3 weekly downloads. As such, @antoniovdlc/defer popularity was classified as not popular.
We found that @antoniovdlc/defer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.