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 () | |
pr.Then (fun x -> printfn "Got value: %A" x) |> ignore | |
pr.Resolve 42 |
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 () | |
pr.Catch (fun err -> printfn "Got Exception: %A" (err.ToString ())) |> ignore | |
pr.Reject (Exception "error") |
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
module Promise = | |
let sleep msecs = | |
let promise = Promise<unit> () | |
let timer = new System.Timers.Timer (float msecs) | |
timer.AutoReset <- false | |
let eventHandler ev = | |
promise.Resolve () | |
timer.Elapsed.Add eventHandler |
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 |
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
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
// 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 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
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
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)) | |
) |