Skip to main content

Defer - API

A defer function defers the execution of a function until the surrounding function returns.

Installation

npm is the default package manager for Node.js, and to where tscommon is published.
Your project is using npm if it has a package-lock.json file in its root folder.

Run the following command in your terminal:

terminal
npm install @tscommon/defer

Usage

main.ts
import { DeferFunction } from '@tscommon/defer';

// It is important to use the `using` keyword here.
using defer = new DeferFunction();

console.log('start');
defer(() => console.log('a'));
defer(() => console.log('b'));
console.log('end');

// Output:
// start
// end
// b
// a