cntx 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/readme.md +8 -6
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
# Async Context For Node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Create global context for async function call chain.
|
|
4
|
+
[](https://badge.fury.io/js/cntx)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
## Purpose
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
`cntx` is a lightweight npm package that simplifies the management of context in nested async functions, particularly in scenarios like request handlers. It eliminates the need for passing context as parameters or relying on global variables, which can lead to errors and conflicts in concurrent environments.
|
|
8
10
|
|
|
9
|
-
`cntx`
|
|
11
|
+
By utilizing Node's async hooks, `cntx` automatically propagates context to the subsequent async functions in the call chain. It offers an easy-to-use API and can serve as a foundation for building custom context managers with multiple namespaces.
|
|
10
12
|
|
|
11
13
|
## Installation
|
|
12
14
|
|
|
@@ -16,7 +18,7 @@ npm install cntx --save
|
|
|
16
18
|
|
|
17
19
|
## Usage
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
To enable async hooks, call `cntx.enable()` at the beginning of your program.
|
|
20
22
|
|
|
21
23
|
```js
|
|
22
24
|
import { cntx } from 'cntx';
|
|
@@ -24,7 +26,7 @@ import { cntx } from 'cntx';
|
|
|
24
26
|
cntx.enable();
|
|
25
27
|
```
|
|
26
28
|
|
|
27
|
-
|
|
29
|
+
You can use `cntx({key: value})` to initialize context for the current async call chain.
|
|
28
30
|
Calling `cntx()` with no arguments will return the current context.
|
|
29
31
|
|
|
30
32
|
### Example
|