-
-
Save filipemacedo/1aac73d4a04e14f6be8924e78420fef9 to your computer and use it in GitHub Desktop.
concurrency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require("axios").default; | |
var data = JSON.stringify({ | |
valor: 1000, | |
tipo: "d", | |
descricao: "descricao", | |
}); | |
var config = { | |
method: "post", | |
url: "http://localhost:9999/clientes/1/transacoes", | |
headers: { | |
"Content-Type": "application/json", | |
}, | |
data: data, | |
validateStatus() { | |
return true; | |
}, | |
}; | |
async function run() { | |
const promises = []; | |
for (let x = 0; x < 100; x++) { | |
promises.push(axios(config)); | |
} | |
const responses = await Promise.all(promises); | |
const result = responses.reduce( | |
(previous, response) => ({ | |
...previous, | |
[response.status]: | |
typeof previous[response.status] === "number" | |
? (previous[response.status] += 1) | |
: 1, | |
[`status_${response.status}`]: [ | |
...(previous[`status_${response.status}`] || []), | |
response.data, | |
], | |
}), | |
{} | |
); | |
console.log(result); | |
} | |
run() | |
.catch(console.error) | |
.then(() => console.log("Done")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment