I am trying to make a function which will take an array of objects and form a table from it. But I want it to be like those promises where we can have promise().then().catch() basically I just want the .then() and .catch() functionality in my function.
I tried doing this:
function makeTable(tableContent) {
tableContent.forEach((cell) => {
let type = cell.type
return new Promise(function(resolve, reject){
if (type === "th" || type === "tr") {
resolve("works")
} else {
reject(`Cell type must be either "th" or "tr" you passed "${type}"`)
}
})
})
}
let table = [
{
type: "pf",
},
]
makeTable(table).then(()=>console.log("Success!")).catch(err=>console.log(err))
But obviously this doesn't work. I wanted to get this functionality cause I just wanted to know more about promises and wanted to make a function which could do the same. Unfortunately, I am unable to find any place on the internet which answers this. Thanks for reading this question!!
Some people are asking if why do I really need to do it even though I can do it other ways. Why only promises? Well, actually I just want to play around with them so that I can better understand them and that's the reason for this question.
forEachdoesn't return anything. You may want to look tomap.