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
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;
});