My background is mostly python so I'm struggling with this async-await paradigm right now. I understand the flow for simple things, but when it's complicated due to nestedness and chaining multiple sync/async pieces I'm not getting the results I was hoping for. Below is the stub that should sum up where I got stuck
const _ = require('underscore');
const axios = require('axios').default;
const func1 = async () => {
let myObj = {1: [1, 2, 3], 2: [1, 2, 3], 3: [1, 2, 3]};
let bigLst = [];
_.each(Object.entries(myObj), async ([k, lst]) => {
let rslt = await Promise.all(lst.map(func2));
bigLst.push(rslt);
});
return bigLst; // but bigLst is empty :'(
};
const func2 = async (id) => {
const rslt = await axios.get(f`https://some-url-${id}.com`);
return rslt;
};
let final_rslt = func1(); // so is final_rslt
func1
doesn't have to return the final result, I can save it in a db or something like that, but the problem is it's still empty inside of the func1
body after the _.each
piece. So my question is - how do I collect results of multiple async calls into a one big object to either return or work with within the function.
Thanks.