Nesting vs Chaining
tip
Chaining is better than nesting in terms of readability
- kotlin-nesting.kt
- kotlin-chaining.kt
stream1
.flatMap {
stream2.flatMap {
stream3.flatMap {
stream4
}
}
}
.collect()
stream1
.flatMap { stream2 }
.flatMap { stream3 }
.flatMap { stream4 }
.collect()
- node-nesting.js
- node-chaining.js
const makeBurger = nextStep => {
getBeef(function(beef) {
cookBeef(beef, function(cookedBeef) {
getBuns(function(buns) {
putBeefBetweenBuns(buns, beef, function(burger) {
nextStep(burger);
});
});
});
});
};
const makeBurger = () => {
return getBeef()
.then(cookBeef)
.then(getBuns)
.then(putBeefBetweenBuns);
};
makeBurger().then(serve);