Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/ayonli_jsext/workerd/index.ts>func

A JavaScript extension package for building strong and modern applications.
Latest
function func
Re-export
import { func } from "https://deno.land/x/ayonli_jsext@v0.9.72/workerd/index.ts";

Inspired by Golang, creates a function that receives a defer keyword which can be used to carry deferred jobs that will be run after the main function is complete.

Multiple calls of the defer keyword is supported, and the callbacks are called in the LIFO order. Callbacks can be async functions if the main function is an async function or an async generator function, and all the running procedures will be awaited.

Examples

Example 1

import func from "@ayonli/jsext/func";
import * as fs from "node:fs/promises";

export const getVersion = func(async (defer) => {
    const file = await fs.open("./package.json", "r");
    defer(() => file.close());

    const content = await file.readFile("utf8");
    const pkg = JSON.parse(content);

    return pkg.version as string;
});

Type Parameters

T
optional
R = any
optional
A extends any[] = any[]

Parameters

fn: (
this: T,
defer: (cb: () => void) => void,
...args: A,
) => R

Returns

(this: T, ...args: A) => R