Skip to content

Instantly share code, notes, and snippets.

@jkachmar
Created June 16, 2016 15:18
Show Gist options
  • Save jkachmar/81434b12d9a75ecc8aeb1e3c2f561e66 to your computer and use it in GitHub Desktop.
Save jkachmar/81434b12d9a75ecc8aeb1e3c2f561e66 to your computer and use it in GitHub Desktop.
Collecting Concurrent Axios Requests
const axios = require('axios');
const CAM_ARR = [
'192.0.2.189',
'192.0.2.188',
'192.0.2.187',
'192.0.2.186',
];
const SLUG = '/slug/goes/here';
function getCams(ip) {
return axios.get(`http://${ip}/${SLUG}`);
}
function getPresence(cams) {
return new Promise((resolve) => {
axios.all(cams.map(getCams))
.then((responses) => {
const data = responses.reduce((acc, each) => {
acc.in += each.data.in;
acc.out += each.data.out;
return acc;
}, { in: 0, out: 0 });
resolve(data);
});
});
}
getPresence(CAM_ARR)
.then((data) => {
console.log(data);
});
import axios from 'axios';
const CAM_ARR = [
'192.0.2.189',
'192.0.2.188',
'192.0.2.187',
'192.0.2.186',
];
const SLUG = '/slug/goes/here';
async function getCams(ip) {
return axios.get(`http://${ip}/${SLUG}`);
}
async function getPresence(cams) {
const resps = await axios.all(cams.map(getCams));
const accum = resps.reduce((acc, resp) => {
acc.in += resp.data.in;
acc.out += resp.data.out;
return acc;
}, { in: 0, out: 0 });
return accum;
}
getPresence(CAM_ARR).then((data) => {
console.log(data);
});
{
"name": "concurrent-axios-reqs",
"version": "0.0.1",
"description": "Test collecting concurrent axios requests",
"main": "index.js",
"private": true,
"dependencies": {
"axios": "~0.12.0"
},
"devDependencies": {
"babel-cli": "~6.10.1",
"babel-preset-es2015": "~6.9.0",
"babel-preset-stage-0": "~6.5.0"
},
"scripts": {
"es5-version": "node es5Version.js",
"es6-version": "babel-node es6Version.js --presets es2015,stage-0"
},
"license": "MIT"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment