map2

JobResult.map2

Namespace: FsToolkit.ErrorHandling

Function Signature:

('a -> 'b -> 'c) -> Job<Result<'a, 'd>> -> Job<Result<'b, 'd>> 
  -> Job<Result<'c, 'd>>

Examples

Note: Many use-cases requiring map operations can also be solved using the jobResult computation expression.

Example 1

Given the functions

getFollowerIds : UserId -> Job<Result<UserId list, exn>>
createPost : CreatePostRequest -> Job<Result<PostId, exn>>

And the type

type NotifyNewPostRequest = 
  { UserIds : UserId list
    NewPostId : PostId }
  static member Create userIds newPostsId =
    {UserIds = userIds; NewPostId = newPostsId}

We can create a NotifyNewPostRequest using JobResult.map2 as below:

let createPostAndGetNotifyRequest (req : CreatePostRequest) = 
  //  Job<Result<UserId list, exn>>
  let getFollowersResult = getFollowerIds req.UserId

  // Job<Result<PostId, exn>>
  let createPostResult = createPost req

  // Job<Result<NotifyNewPostRequest, exn>>
  let newPostRequestResult =
    JobResult.map2 
      NotifyNewPostRequest.Create getFollowersResult createPostResult

  // ...

Last updated