How would you recommend I go about fetching multiple files (in particular, CSV files) and merging them into a single variable? I've tried using the following and I'm pretty sure I got something about the promise syntax/logic wrong, as that is an area I'm fairly new to, but it may be another problem. Here is my current attempt:
fetcher(specURL).then(async (spec) => {
let rawData;
if (typeof spec.dataURL === "string") {
rawData = await fetcher(spec.dataURL);
} else if (typeof spec.dataURL === "array") {
rawData = {};
await spec.dataURL.forEach(async (url, i) => {
rawData[i] = await fetcher(url);
});
}
console.log(rawData);
}
As you can see I use a spec (specification) json which articulates which URLs to fetch. If it is an array I would like to fetch multiple files into an array.
I'm using my fetcher function, which is the following (any recommendations to improving it are appreciated too!
async function fetcher(url, type) {
let fetched = await fetch(url);
fetched = await fetched.text();
if (type == undefined) {
type = url.split(".").pop();
}
if (type == "json") {
fetched = JSON.parse(fetched);
} else if (type == "csv") {
fetched = Papa.parse(fetched, { header: true }).data;
}
return fetched;
}