Avoiding pyramid of doom

Pyramid of doom is a classic scenario where we have tons of nesting or branching. This makes the code overly complex and the unit testing a very complex job:

promise1()
.then((resp) => {
promise2(resp)
.then((resp2) => {
promise3(resp2)
.then((resp3) => {
if(resp3.something) {
// do something
} else {
// do something else
}
});
});
});

Instead, do the following:

promise1()
.then((resp) => {
return promise2(resp);
})
.then((resp2) => {
return promise3(resp2);
})
.then((resp3) => {
if(resp3.something) {
// do something
} else {
// do something else
}
})

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.187.155