UNPKG

688 BMarkdownView Raw
1# A cls session implementation use async_hooks
2
3Continuation Local Storage works like thread-local storage in threaded programming. This is a implementation of CLS using async_hooks instead of async-listener.
4
5It need running nodejs version >= 8.2.1.
6
7## Example
8
9``` js
10const Session = require('./index')
11
12const session = new Session()
13
14function timeout (id) {
15 session.scope(() => {
16 session.set('a', id)
17 setTimeout(() => {
18 const a = session.get('a')
19 console.log(a)
20 })
21 })
22}
23
24timeout(1)
25timeout(2)
26timeout(3)
27
28// Output:
29// 1
30// 2
31// 3
32```
33
34## Class: Session
35
36### session.scope(callback)
37
38### session.set(key, value)
39
40### session.get(key)
41
42### session.context
43