function-contexts
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Function Contexts

React like contexts for sync and async functions.

Example

import { createFunctionContext, getFunctionContext } from "function-contexts";

const kMyContext = createFunctionContext<number>();

function printContextValue() {
    console.log(`MyContext value: ${getFunctionContext(kMyContext)}`)
}

async function withContext() {
    printContextValue();
    await new Promise(resolve => setTimeout(resolve, 1000));
    printContextValue();
}

async function main() {
    await kMyContext.run(withContext, 1234);
}

main().catch(error => {
    console.error("main() encountered a critical error: %o", error);
});

Readme

Keywords

none

Package Sidebar

Install

npm i function-contexts

Weekly Downloads

8

Version

1.0.1

License

MIT

Unpacked Size

12.2 kB

Total Files

14

Last publish

Collaborators

  • wolverindev