Best practice
Keep code left
This long wrapping in a if / else clause makes the code hard to read.
Bad
if (condition) {
doWork();
} else {
throw Error("Condition not met!");
}Good
if (!condition) {
throw Error("Condition not met!");
}
doWork();Stop nesting promises
Bad
getData((a) => {
getMoreData(a, (b) => {
getMoreData(b, (c) => {
getMoreData(c, (d) => {
getMoreData(d, (e) => {
console.log(e);
});
});
});
});
});Better
getData()
.then((a) => getMoreData(a))
.then((b) => getMoreData(b))
.then((c) => getMoreData(c))
.then((d) => getMoreData(d))
.then((e) => console.log(e));Even better
const a = await getData();
const b = await getMoreData(a);
const c = await getMoreData(b);
const d = await getMoreData(c);
const e = await getMoreData(d);
console.log(e);