map2

Task.map2

Namespace: FsToolkit.ErrorHandling

Function Signature:

('a -> 'b -> 'c) -> Task<'a> -> Task<'b> -> Task<'c>

Examples

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

Example 1

Given the functions

getFollowerIds : UserId -> Task<UserId list>
createPost : CreatePostRequest -> Task<PostId>

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 Task.map2 as below:

let createPostAndGetNotifyRequest (req : CreatePostRequest) = 
  //  Task<UserId list>
  let getFollowersResult = getFollowerIds req.UserId

  // Task<PostId>
  let createPostResult = createPost req

  // Task<NotifyNewPostRequest>
  let newPostRequestResult =
    Task.map2 
      NotifyNewPostRequest.Create getFollowersResult createPostResult

  // ...

Last updated