Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/istarkov/async-decorators
async-await and promise class methods decorators
https://github.com/istarkov/async-decorators
Last synced: about 2 months ago
JSON representation
async-await and promise class methods decorators
- Host: GitHub
- URL: https://github.com/istarkov/async-decorators
- Owner: istarkov
- Created: 2015-06-02T08:38:13.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-10-30T17:22:18.000Z (about 9 years ago)
- Last Synced: 2024-10-08T01:47:22.773Z (2 months ago)
- Language: JavaScript
- Size: 141 KB
- Stars: 43
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
There are some helpfull decorators in this project for async class methods and functions.
#Install
```shell
npm install --save async-decorators
```#Import
```
import {memoize, serialize, isSkipError} from 'async-decorators';
```#Memoize decorator
has parameters `expireMs` and `cacheSize`
usage:
```javascript
class Action {
@memoize({expireMs: K_EXPIRE_MS, cacheSize: 256})
async query(p1, p2) {
return await getDataAsync({p1, p2});
}
}
```
or just```javascript
const asyncFn = memoize(async (x) => {
return await ....
});
```See [example source](https://github.com/istarkov/async-decorators/blob/master/examples/memoize.js)
```
npm run example_memoize
```
and [test source](https://github.com/istarkov/async-decorators/blob/master/__tests__/memoize.js)
```
npm run test_memoize
```#Serialize decorator
Serializes async method calls. (Make a new async call only if previous is completed)
If there are more than one pending async calls, skip all but the last.usage:
```javascript
class Action {
@serialize()
async query(p1, p2) {
return await getDataAsync({p1, p2});
}
}
```or just
```javascript
const sfn = serialize(async (x) => {
return await ....
})
```See [example source](https://github.com/istarkov/async-decorators/blob/master/examples/serialize.js)
```
npm run example_serialize
```#Both decorator usage
```javascript
class Action {
@serialize()
@memoize({expireMs: K_EXPIRE_MS})
async query(p1, p2) {
return await getDataAsync({p1, p2});
}
}
```See [example source](https://github.com/istarkov/async-decorators/blob/master/examples/ser_memoize.js)
```
npm run example_ser_memoize
```