https://github.com/cristidraghici/river-functions
Execute multiple asynchronous functions and when they are finished execute the callback
https://github.com/cristidraghici/river-functions
Last synced: 3 months ago
JSON representation
Execute multiple asynchronous functions and when they are finished execute the callback
- Host: GitHub
- URL: https://github.com/cristidraghici/river-functions
- Owner: cristidraghici
- License: mit
- Created: 2017-07-05T08:52:40.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-07-07T09:20:45.000Z (almost 8 years ago)
- Last Synced: 2024-03-24T21:21:18.526Z (about 1 year ago)
- Language: JavaScript
- Size: 20.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# river-functions
Execute multiple asynchronous functions and when they are finished execute the callback with result parameters from the functions run.
## Important note
This package is still under active development. Suggestions and issue reports are more than welcome!
## Install
`npm install river-functions`
## Development
- `npm install`
- `npm install babel-cli -g`## Testing
`npm run test`
## Full example usage
```javascript
import river from 'river-functions'
import {each} from 'lodash'// Log the errors
const log = console.log;// Demo function to execute asynchronously
const asyncFunction = (data, callback) => {
const err = Math.floor((Math.random() * 10) + 1) === 1; // random between 1 and 10\. If 1, error is trueif (err) {
return callback(err);
}return callback(null, data);
}// Function to add to the river queue
const addToRiver = (datafn) => {
return (riverTaskDoneCallback) => {
asyncFunction(datafn, riverTaskDoneCallback);
}
}// Demo records
const records = [
{
name: 'Item 1',
value: 'Value 1'
}, {
name: 'Item 2',
value: 'Value 2'
}, {
name: 'Item 3',
value: 'Value 3'
}
];// Go through each of the records and store them in the river array
const functionsRiver = [];each(records, (item, idx) => {
functionsRiver.push(addToRiver(item));
});// Execute the river
river(functionsRiver, (status, results, errors) => {
if (status === false) {
return log('Error encountered: ', errors);
}// Close the job
log('Successful execution: ', results);
});
```