What is the cleanest way work with a chain of `Opt...
# announcements
m
What is the cleanest way work with a chain of
Optionals
when you need to hang on to intermediate values? (“Promise Hell” in JavaScript) Sample:
Copy code
return receiptRepository.findOne(receiptId)
    .map { lineRepository.findOne(it, lineId) }
    .map { lineRepository.update( /* need receipt here */, it, updatedLine) }
    .map { ResponseEntity(it, HttpStatus.OK ) }
    .orElse { ResponseEntity(HttpStatus.NOT_FOUND) }
a
.map { lineRepository.findOne(it, lineId) to it }
?
or
Copy code
.map { 
    val line = lineRepository.findOne(it, lineId)
    lineRepository.update(it, line, updatedLine)
}
m
Busy missing the trees for the forest. THank you!
m
This is where for-comprehension syntax would be nice. Like in this Scala'ish code:
Copy code
val response = for {
  receipt <- receiptRepository.findOne(receiptId)
  line <- lineRepository,findOne(receipt, lineId)
  lineUpdate <- lineRepository.update(receipt, line, updatedLine)
} yield ResponseEntity(lineUpdate, HttpStatus.OK)

return response.orElse { ResponseEntity(HttpStatus.NOT_FOUND) }
m
yeah, that’d be nice!
g
#C5UPMM0A0 has
bindings
that allow to write such code much easier
👍🏼 1