npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dacodedbeat/with-defer-js

v0.0.2

Published

A simple and lightweight JavaScript library for scheduling deferred function callbacks to execute after a function, like Golang.

Downloads

140

Readme

with-defer.js

with-defer.js is a simple and lightweight JavaScript library that allows you to schedule function callbacks after the execution of a function, just like Golang’s defer. Perfect for cleanup tasks, error handling, and more for both asynchronous and synchronous operations.

Why with-defer.js?

Inspired by Golang’s defer, this library gives you the same power in JavaScript.

In Go, defer lets you schedule a function to run after the current function ends - whether it finishes normally or with an error. This is useful for tasks like closing files or releasing resources without messing up the main flow of the program.

In JavaScript, especially with async operations, you often need similar cleanup tasks, but there's no built-in defer keyword. This library brings the simplicity of Go's defer to JavaScript.

TL;DR: Stack up tasks, run them after your main code finishes (even if it crashes), and keep your code tidy.

Golang vs. with-defer.js

Golang:

func main() {
    defer cleanup1()
    defer cleanup2()
    // do stuff
}

with-defer.js:

import { withDefer } from 'with-defer';

const mainFunction = (defer) => {
    defer(() => console.log("Cleanup 1"));
    defer(() => console.log("Cleanup 2"));
    // do stuff
};

withDefer(mainFunction)();

How the two align

  1. Automatic Execution After Function Ends: Just like in Go, functions in with-defer.js run after the main function finishes, whether it succeeds or fails. This makes sure cleanup tasks are done.

  2. Last-In, First-Out Execution: Deferred functions in both Go and with-defer.js run in reverse/LIFO(Last-In, First-Out) order, meaning the last function added runs first. This helps with cleaning up resources in the correct order.

  3. User-Friendly API: The defer() in with-defer.js is similar to Go’s, making it easy to use for scheduling functions in JavaScript.

Same vibe, right?

Features

  • Stack up deferred functions to run after your main code.
  • Set timeouts and cancel deferred tasks.
  • Built-in error reporting with optional error throwing.
  • Optional debug mode for the curious souls.

Install

npm install with-defer

Usage

Wrap your function with withDefer and use defer to schedule tasks.

import { withDefer } from 'with-defer';

const mainFunction = async (defer) => {
    defer(() => console.log('Deferred task!'), { timeout: 5000 });

    console.log('Doing main stuff!');
};

withDefer(mainFunction)();

Options

[!IMPORTANT] When implementing, please verify this with the your installed version of the source code, as this is can change over time.

Set options when you wrap your function, as well as on each deferred callback:

| Option | Type | Default | Description | |-----------------|-----------|----------|---------------------------------------------------------------------------| | timeout | number | null | Max time (ms) for deferred functions. | | debug | boolean | false | Debug mode. | | throwOnError | boolean | false | Throw error if a deferred function fails. | | errorReporter | function| null | Custom error handler for deferred functions. |

Error Handling & Debugging

  • Use throwOnError to fail hard if a deferred function crashes.
  • Use errorReporter for custom error handling.
  • Enable debug to get logs.

Cancel a Deferred Task

const { cancel } = defer(() => console.log('This won’t run if canceled.'));
cancel();

Contributing

If you’d like to contribute to with-defer.js, we welcome your input! Please read our Contributing Guidelines to get started.

License

with-defer.js is licensed under the Mozilla Public License 2.0. See the LICENSE file for more details.