Asynchronous programming

Use the full power of F# asynchronous workflows

Simple Async Demo

×
 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
[<ReflectedDefinition>]
module Program

open FunScript
open FunScript.TypeScript
open System.Threading

// ----------------------------------------------------------------------------
// Mini implementation of some F# async primitives

type Async =
  static member AwaitJQueryEvent(f : ('T -> obj) -> JQuery) : Async<'T> = 
    Async.FromContinuations(fun (cont, econt, ccont) ->
      let named = ref None
      named := Some (f (fun v -> 
        (!named).Value.off() |> ignore
        cont v
        obj() )))
      
// ----------------------------------------------------------------------------
// Demo using mini F# async

let j (selector : string) = Globals.Dollar.Invoke(selector)
let (?) jq name = jq("#" + name)

let log(msg:string) =
   let tag = "<p>" + msg + "</p>"
   j?results.append [| tag :> obj |]
   |> ignore

let increment(n) = 
  async {
    do! Async.Sleep(1000)
    return n + 1 
  }

let rec worker(n) = 
  async { 
    let! v = Async.AwaitJQueryEvent(fun f -> j?next.click(fun x -> f x))
    let! n = increment(n)
    do log ("Count: " + n.ToString())
    return! worker(n)
  }

let main() = 
  async {
    let! x = Async.AwaitJQueryEvent(fun o -> j?document.ready(unbox<Function> o))
    let cts = new CancellationTokenSource()
    Async.StartImmediate(worker 0, cts.Token)
    j?stop.click(fun _ -> box <| cts.Cancel()) |> ignore
  } |> Async.StartImmediate


// ----------------------------------------------------------------------------

do Runtime.Run()
Multiple items
type ReflectedDefinitionAttribute =
  inherit Attribute
  new : unit -> ReflectedDefinitionAttribute

Full name: Microsoft.FSharp.Core.ReflectedDefinitionAttribute

--------------------
new : unit -> ReflectedDefinitionAttribute
module Program
namespace FunScript
namespace FunScript.TypeScript
namespace System
namespace System.Threading
Multiple items
type Async =
  static member AwaitJQueryEvent : f:(('T -> obj) -> JQuery) -> Async<'T>

Full name: Program.Async

--------------------
type Async<'T>

Full name: Microsoft.FSharp.Control.Async<_>
static member Async.AwaitJQueryEvent : f:(('T -> obj) -> JQuery) -> Async<'T>

Full name: Program.Async.AwaitJQueryEvent
val f : (('T -> obj) -> JQuery)
type obj = System.Object

Full name: Microsoft.FSharp.Core.obj
type JQuery

Full name: FunScript.TypeScript.JQuery
static member Async.FromContinuations : callback:(('T -> unit) * (exn -> unit) * (System.OperationCanceledException -> unit) -> unit) -> Async<'T>
val cont : ('T -> unit)
val econt : (exn -> unit)
val ccont : (System.OperationCanceledException -> unit)
val named : JQuery option ref
Multiple items
val ref : value:'T -> 'T ref

Full name: Microsoft.FSharp.Core.Operators.ref

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
union case Option.None: Option<'T>
union case Option.Some: Value: 'T -> Option<'T>
val v : 'T
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val j : selector:string -> JQuery

Full name: Program.j
val selector : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
type Globals

Full name: FunScript.TypeScript.Globals
property Globals.Dollar: JQueryStatic
member JQueryStatic.Invoke : unit -> JQuery
member JQueryStatic.Invoke : element:Element -> JQuery
member JQueryStatic.Invoke : elementArray:Element array -> JQuery
member JQueryStatic.Invoke : _object:AnonymousType436 -> JQuery
member JQueryStatic.Invoke : _object:JQuery -> JQuery
member JQueryStatic.Invoke : callback:Function -> JQuery
member JQueryStatic.Invoke : selector:string * ?context:Element -> JQuery
member JQueryStatic.Invoke : html:string * attributes:Object -> JQuery
val jq : (string -> 'a)
val name : string
val log : msg:string -> unit

Full name: Program.log
val msg : string
val tag : string
val increment : n:int -> Async<int>

Full name: Program.increment
val n : int
val async : AsyncBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
static member Async.Sleep : millisecondsDueTime:int -> Async<unit>
val worker : n:int -> Async<'a>

Full name: Program.worker
val v : JQueryEventObject
static member Async.AwaitJQueryEvent : f:(('T -> obj) -> JQuery) -> Async<'T>
val f : (JQueryEventObject -> obj)
val x : JQueryEventObject
System.Int32.ToString() : string
System.Int32.ToString(provider: System.IFormatProvider) : string
System.Int32.ToString(format: string) : string
System.Int32.ToString(format: string, provider: System.IFormatProvider) : string
val main : unit -> unit

Full name: Program.main
val x : obj
val o : (obj -> obj)
val unbox : value:obj -> 'T

Full name: Microsoft.FSharp.Core.Operators.unbox
type Function

Full name: FunScript.TypeScript.Function
val cts : CancellationTokenSource
Multiple items
type CancellationTokenSource =
  new : unit -> CancellationTokenSource + 2 overloads
  member Cancel : unit -> unit + 1 overload
  member CancelAfter : delay:TimeSpan -> unit + 1 overload
  member Dispose : unit -> unit
  member IsCancellationRequested : bool
  member Token : CancellationToken
  static member CreateLinkedTokenSource : params tokens:CancellationToken[] -> CancellationTokenSource + 1 overload

Full name: System.Threading.CancellationTokenSource

--------------------
CancellationTokenSource() : unit
CancellationTokenSource(delay: System.TimeSpan) : unit
CancellationTokenSource(millisecondsDelay: int) : unit
static member Async.StartImmediate : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
property CancellationTokenSource.Token: CancellationToken
val box : value:'T -> obj

Full name: Microsoft.FSharp.Core.Operators.box
CancellationTokenSource.Cancel() : unit
CancellationTokenSource.Cancel(throwOnFirstException: bool) : unit

Live demo

Click here to open the sample live, running using JavaScript in a dialog window.

To look at the generated JavaScript, view the source of this web page and scroll to the end of the file.

Tutorials

Learn FunScript from the following introductory tutorials

Samples

Browse other amazing samples created with FunScript

Fork me on GitHub