Skip to content

Instantly share code, notes, and snippets.

@jhoerr
Last active December 11, 2018 23:26
Show Gist options
  • Save jhoerr/01c9b2429bb366a400a42228366c2aa9 to your computer and use it in GitHub Desktop.
Save jhoerr/01c9b2429bb366a400a42228366c2aa9 to your computer and use it in GitHub Desktop.
An approach to tying together sync/async Chessie operations.
open Chessie.ErrorHandling
// A 'normal' synchronous function
let f a =
ok a
// A 'normal' async expression
let fAsync a = async {
return ok a
}
// A synchronous ROP trial
let fTrial a = trial {
return a
}
// An async ROP trial
let fAsyncTrial a = asyncTrial {
return a
}
// An awaiter for normal async functions
let await f x =
f x
|> Async.RunSynchronously
// An awaiter for async trials
let awaitTrial f x =
f x
|> Async.ofAsyncResult
|> Async.RunSynchronously
// Take a Result and generate an HTTP response
let constructResponse r =
match r with
| Ok(x,msgs) -> "HTTP 200" // do something on success
| Bad(msgs) -> "HTTP 500" // do something on failure
// Execute a workflow with a variety of sync/async operations.
// The >>= operator attempts to bind the 'Success' object of the previous
// function and passes it to the next function in the workflow. If at any point
// the bind fails, then we fall through to the 'constructResponse' function.
let workflow request =
request
|> f
>>= fTrial
>>= await fAsync
>>= awaitTrial fAsyncTrial
|> constructResponse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment