why do you need to explicitly return an HttpHandler when you need the HttpContext? #475
              
                
                  
                  
                    Answered
                  
                  by
                    dustinmoris
                  
              
          
                  
                    
                      Melomaniash
                    
                  
                
                  asked this question in
                Q&A
              
            -
| i'm confused as to why the docs state that you have to return an httphandler explicitly when you need the context. bellow i wrote the function as stated in the docs and then tried writing it directly into the function. let addExampleRecord =
        fun (next : HttpFunc) (ctx : HttpContext) ->
            task {
                let! exampleRecord = ctx.BindJsonAsync<ExampleRecord>()
                do! ExampleDataModule.storeExampleRecord exampleRecord
                return! Successful.OK exampleRecord next ctx
            }
let addExampleRecord2 (next : HttpFunc) (ctx : HttpContext) =
    task {
        let! exampleRecord = ctx.BindJsonAsync<ExampleRecord>()
        do! ExampleDataModule.storeExampleRecord exampleRecord
        return! Successful.OK exampleRecord next ctx
    }Both functions worked. So my question is, is there a difference between these 2 functions? and if there is, what is it? | 
Beta Was this translation helpful? Give feedback.
      
      
          Answered by
          
            dustinmoris
          
      
      
        May 5, 2021 
      
    
    Replies: 1 comment
-
| Hi, great question and the answer is that there is no difference. However, there are two reasons which I can think of why some people prefer the first case: 
 Example: let addExampleRecord (foo : string) (bar : int) =
        // Do some work only once here using foo and bar
        let fooBar = foo + bar.ToString()
        fun (next : HttpFunc) (ctx : HttpContext) ->
            task {
                let! exampleRecord = ctx.BindJsonAsync<ExampleRecord>()
                do! ExampleDataModule.storeExampleRecord exampleRecord
                // Use fooBar here...
                return! Successful.OK exampleRecord next ctx
            } | 
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            
      Answer selected by
        Melomaniash
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
Hi, great question and the answer is that there is no difference.
However, there are two reasons which I can think of why some people prefer the first case:
HttpHandlerand then let the linter and compiler help you to put the curried parameters in the correct order and make sure that the return type matches what aHttpHandlerexpects. It's a facility to help write correct code.nextandctxthen you could run some work in advance before theHttpHandlerpart is being returned.Example: