I have the following code:
for (const element of elements) {
try {
// eslint-disable-next-line no-await-in-loop
const { a, b, c, d, e } = await func(element.id);
if (tokens.includes(a)) {
saveElement("a", a, element.id); // First arg is the key as string
break;
}
if (tokens.includes(b)) {
saveElement("b", b, element.id);
break;
}
...
if (tokens.includes(e)) {
saveElement("e", e, element.id);
break;
}
} catch (err) {
console.error(err);
}
}
As you can imagine, there might be a way to avoid this long if statement queue... If you see, there is some kind of pattern in the code, where the only things which are different are the fields destructured from the result of func().
Is there any way to make this cleaner? I mean, to avoid the spaghetti code.
Thank you.