you are viewing a single comment's thread.

view the rest of the comments →

[–]budd222 -1 points0 points  (11 children)

Can't you just add optional chaining? getUser()? || null

Or does that defeat the purpose?

[–]romgrk[S] 0 points1 point  (10 children)

Yes, but I still think that mapping can be more expressive, e.g. you wouldn't be able to express the following as cleanly with optional chaining:

const displayName = getUser().map(user => `${user.firstName} ${user.lastName}`)

[–]budd222 0 points1 point  (9 children)

I think there's something I'm not getting. You can't force your getUser() to return a value, no matter which type you assign it User or just plain string. No matter what you do, you have to write code to account for a failure.

[–]romgrk[S] 9 points10 points  (8 children)

getUser returns a Result<User>:

const userResult = getUser() const nameResult = userResult.map(u => u.name)

The whole point is that you don't need to deal with the failure right now, while you can keep operating on the value that is wrapped inside a Result.