Skip to content

Instantly share code, notes, and snippets.

@legndery
Created June 22, 2019 20:26
Show Gist options
  • Save legndery/59e1f51c7f4870ce06b60f55e37cb478 to your computer and use it in GitHub Desktop.
Save legndery/59e1f51c7f4870ce06b60f55e37cb478 to your computer and use it in GitHub Desktop.
Using async-await
const axios = require('axios');
const empIds = ['72632', '72633', '72634'];
const dummyRESTurl = 'http://dummy.restapiexample.com/api/v1/employee/';
const result = [];
//Serial
(async () => {
for (let i = 0; i < empIds.length; i++) {
const body = await axios.get(`${dummyRESTurl}${empIds[i]}`);
result.push(body.data);
}
console.log(result);
})()
//Parallel
const promises = [];
empIds.forEach((empID) => {
promises.push(axios.get(`${dummyRESTurl}${empID}`));
});
(async () => {
const pResult = await Promise.all(promises);
console.log(pResult.map(r => r.data))
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment