Async and arrays
JavaScript
function api (index) {
return new Promise(resolve => setTimeout(resolve, (2 - index) * 100))
}
async function main () {
const results = [];
await Promise.all([
(async () => {
await api(0);
results.push(0);
})(),
(async () => {
await api(1);
results.push(1);
})(),
]);
console.log(results);
}
main()