https://github.com/leecjson/node-combine-result
Combine results
https://github.com/leecjson/node-combine-result
Last synced: 11 months ago
JSON representation
Combine results
- Host: GitHub
- URL: https://github.com/leecjson/node-combine-result
- Owner: leecjson
- License: mit
- Created: 2018-11-26T14:18:51.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-11-26T15:21:17.000Z (over 7 years ago)
- Last Synced: 2025-06-23T06:16:23.724Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 2.93 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# node-combine-result
# Usage
Combine a few call to a single result which it's returned by first call.
Consider you have a few requests to reads a same file that is very huge let's say it about 1GB and assume it's a fixed file; in that case, u don't want to read the file in each request, so let's combine those requests to read the file just once.
```shell
npm install --save combine-result
```
```javascript
const readFile = path => {
return new Promise(resolve => {
setTimeout(() => resolve('huge content'), 1000); // 1Gb file, read it cost 1000ms
});
};
const combine = requrie('combine-result')();
const readFileCombined = path => {
return combine(path, () => readFile(path));
}
// The following three will combine, read 'a.txt' and return it three times
readFileCombined('a.txt').then(val => console.log(val), err => console.log(err));
readFileCombined('a.txt').then(val => console.log(val), err => console.log(err));
readFileCombined('a.txt').then(val => console.log(val), err => console.log(err));
// The following two are combined
readFileCombined('b.txt').then(val => console.log(val), err => console.log(err));
readFileCombined('b.txt').then(val => console.log(val), err => console.log(err));
setTimeout(() => {
// Reread 'a.txt' since lastest read 'a.txt' calls has returned.
readFileCombined('a.txt').then(val => console.log(val), err => console.log(err));
// The following three are combined
readFileCombined('c.txt').then(val => console.log(val), err => console.log(err));
readFileCombined('c.txt').then(val => console.log(val), err => console.log(err));
readFileCombined('c.txt').then(val => console.log(val), err => console.log(err));
}, 1500);
```