This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Promise<'a> () = | |
let mutable result : Result<'a, exn> option = None | |
let catchCbs = List<Callback<exn>> () | |
let thenCbs = List<Callback<'a>> () | |
member this.Resolve (value: 'a) = | |
result <- Some <| Ok value | |
for cb in thenCbs do | |
scheduler.Post (fun () -> cb value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rec factorial (n : bigint) : Promise<bigint> = | |
let pr = Promise<bigint> () | |
pr.Resolve n | |
pr.Then (fun value -> | |
if value = (bigint 0) then | |
single (bigint 1) | |
else | |
factorial(n - (bigint 1)).Then (fun res -> single (n * res)) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Computation = unit -> unit | |
let scheduler = MailboxProcessor<Computation>.Start (fun inbox -> | |
let rec loop () = async { | |
let! action = inbox.Receive() | |
action () | |
do! loop () | |
} | |
loop () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let pr = promise { | |
let! res1 = fetch "http://test1" | |
let! res2 = fetch "http://test2" | |
let! res3 = fetch "http://test3" | |
let! res4 = fetch "http://test3" | |
return res4 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type QueryBuilder() = | |
member this.Zero () = Promise () | |
member this.ReturnFrom (xs: Promise<'a>) = xs | |
member this.Return x = | |
let promise = Promise<'a> () | |
promise.Resolve x | |
promise | |
member this.Bind(source: Promise<'a>, fn: 'a -> Promise<'b>) : Promise<'b> = source.Then fn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch("http://test1") | |
.Then(fun res1 -> fetch "http://test2") | |
.Then(fun res2 -> fetch "http://test3") | |
.Then(fun res3 -> fetch "http://test4") | |
.Then(fun res4 -> | |
printfn "Got response %A" res4 | |
) | |
|> ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Make a new extension method on Promise | |
type Promise<'a> with | |
member this.Then(fn: 'a -> Promise<'b>) : Promise<'b> = | |
let newPromise = Promise<'b> () | |
this.Then(fun a -> | |
let tmpPromise = fn a | |
tmpPromise.Then(newPromise.Resolve).Catch(newPromise.Reject) | |
|> ignore | |
).Catch(newPromise.Reject) | |
|> ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch("http://test1") | |
.Then(fun res1 -> | |
fetch("http://test2") | |
.Then(fun res2 -> | |
fetch("http://test3") | |
.Then(fun res3 -> | |
fetch("http://test4") | |
.Then(fun res4 -> | |
printfn "Got last response: %A" res4 | |
) |> ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let pr = fetch "http://test" | |
pr.Then (fun value -> | |
printfn "Got response from: %A" value | |
) |> ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fetch(uri: string) = | |
let timeout = Promise.sleep 100 | |
let response = sprintf "Got response from: %s" uri | |
let result = Promise<string> () | |
timeout.Then(fun _ -> result.Resolve(response) ) |> ignore | |
result |