https://github.com/derrickpelletier/res-async
Express async middleware helpers to reduce endpoint complexity
https://github.com/derrickpelletier/res-async
Last synced: 24 days ago
JSON representation
Express async middleware helpers to reduce endpoint complexity
- Host: GitHub
- URL: https://github.com/derrickpelletier/res-async
- Owner: derrickpelletier
- License: mit
- Created: 2013-12-19T17:05:37.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-12-19T17:07:39.000Z (over 11 years ago)
- Last Synced: 2025-03-05T08:33:36.132Z (about 2 months ago)
- Language: JavaScript
- Size: 102 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Express async helpers to reduce some endpoint complexity. Injected into res via middleware.
-------
Available methods:
+ `res.renderWhenDone(view, functionHash)`
+ `res.jsonWhenDone(functionHash)`
+ `res.asyncProcess(functionHash, callback)`The function hash is a number of properties whose values are functions with a callback pattern of (err, result). Upon completion the hash is transformed into the results of each function, and handled according to the helper called:
+ `renderWhenDone` provides the hash to the view.
+ `jsonWhenDone` returns the hash to the client as json.
+ `asyncProcess` returns the hash to the callback given.The function hash can contain non-function entities which will be merged into the final result after the functions themselves have been processed. If your function requires arguments, pass them in as an array wherein the first item is the function. See below (or test.js) for an example.
-----
## Using
```
npm install res-async
```Setup:
```javascript
//... app configapp.use(express.methodOverride());
app.use(require('res-async'));
app.use(app.router);//... more app config
```Use (see test.js):
```javascript
app.get('/cool', function (req, res) {
res.renderWhenDone('somecoolview', {
propOne: someAsyncMethod,
justAString: "I'm not a function",
faveFood: someAsyncQuery,
argsExample: [delaySquare, 5]
});});
```