I have a JSON file wth a list of markdown values, I'm using Remark to parse them to HTML. After each value is parsed I add it to an object so it's accessible, however I think I'm getting stuck somewhere in promise land because the values in the object are undefined.
let sections = {};
const parseMD = (val) => {
let parse = new Promise((resolve, reject) => {
const file =
unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeStringify)
.process(val.body);
resolve(file);
});
parse.then(
function (str) {
sections[val.id] = str.value;
}
);
return parse;
}
contents.forEach((val) => parseMD(val).then((val) => val).catch(err => console.log(err)));
As far as I can tell this should work... If I add a console.log(sections)
I get {}
, which looks like an empty object but when I expand it I see that all my content is in it:
section1: "blah blah blah",
section2: "something else"
...
But sections[section1]
will be undefined
.
This feels like I'm getting held up in the promise somewhere but I don't know how to fix it.
I've also tried this with async/await
in the for loop: content.sections.forEach(async (val) => await parseMD(val));
same results.