https://github.com/goto-bus-stop/async-cls
dead simple continuation local storage using async hooks
https://github.com/goto-bus-stop/async-cls
Last synced: 4 months ago
JSON representation
dead simple continuation local storage using async hooks
- Host: GitHub
- URL: https://github.com/goto-bus-stop/async-cls
- Owner: goto-bus-stop
- License: other
- Created: 2018-05-23T15:05:40.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-07-11T12:00:44.000Z (almost 6 years ago)
- Last Synced: 2025-02-13T14:18:17.857Z (5 months ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# async-cls
dead simple "continuation-local storage" with Node async_hooks
[![npm][npm-image]][npm-url]
[![travis][travis-image]][travis-url]
[![standard][standard-image]][standard-url][npm-image]: https://img.shields.io/npm/v/async-cls.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/async-cls
[travis-image]: https://img.shields.io/travis/goto-bus-stop/async-cls.svg?style=flat-square
[travis-url]: https://travis-ci.org/goto-bus-stop/async-cls
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[standard-url]: http://npm.im/standard## Install
```
npm install async-cls
```## Usage
continuation local storage allows you to implicitly make values available to an asynchronous chain of code.
Multiple asynchronous chains of the same code can be running simultaneously, and each will only get access to its own value.```js
var cls = require('async-cls')
var express = require('express')var userContext = cls()
var app = express()
app.get('/', function (req, res) {
userContext.current = req.user
// you can do any async operation, like filesystem access or waiting for Promises
// to resolve
setTimeout(function () {
respond(res)
})
})function respond (res) {
// `userContext.current` will return the `req.user` value that was set in this async call stack
res.json({ userId: userContext.current.id })
}
```## API
### `context = cls()`
Create a new context. A context can hold a single JavaScript value per async context.
### `context.current = value`
Set the value for this async context (call stack).
### `context.current`
Get the value for this async context.
### `context.destroy()`
Destroy the context, removing its `async_hook` and cleaning up any context values. Accessing `context.current` after the context has been destroyed will return `undefined`.
## License
[Apache-2.0](LICENSE.md)