B4J Tutorial [BANano] Resumeable Sub (sort of)

Discussion in 'B4J Tutorials' started by alwaysbusy, Dec 17, 2018.

  1. alwaysbusy

    alwaysbusy Expert Licensed User

    Javascript acts differently compared to java when doing some async stuff and is rather complex. However, I've tried to make it as comfortable as possible for you, the programmer. All you have to do is follow some very simple rules.

    Maybe first an example:
    Code:
    dim RoomIsCleaned as boolean = true
    ...

    Sub Btn2_Clicked()
       BANano.Msgbox(
    "btn2 clicked through BANanoObject and jQuery!")
       
    Dim promiseResult As Map
       BANano.WaitFor(promiseResult, Me, 
    "CheckTheRoomWait")
       
    Log("Wait response: " & promiseResult.Get("goodChild") & " --> " & promiseResult.Get("result"))
    End Sub

    public Sub CheckTheRoomWait(resolve As Object)
       BANano.Sleep(
    3000)
       
    Dim m As Map
       m.Initialize
       
    If RoomIsCleaned Then
           m.Put(
    "goodChild"True)
           m.Put(
    "result""The room is cleaned!")
       
    Else
           m.Put(
    "goodChild"False)
           m.Put(
    "result""The room is not cleaned!")
       
    End If
       BANano.Resolve(m)
    End Sub
    What we do here is running a method (CheckTheRoomWait), which pauses the code for 3 seconds and then returns an answer. In this case it returns a Map, but it can be a simple string too.

    We can call our CheckTheRoomWait method with BANano.Waitfor().

    Some important rules:

    The syntax of the ...Wait method is very important:
    Code:
    [YourFuncName]Wait(resolve As Object)
    1. The called method name has to end with Wait
    2. It can not have a return value: use BANano.Resolve() to return your answer
    3. you can not add extra parameters, nor change the name of the resolve variable

    In case you are building a library:
    1. if you use BANano.WaitFor or BANano.Sleep() in a method, the containing method must end with Wait too.

    I've build-in some warnings in which will guide you if you forgot the Wait.

    Note: a WaitFor does not 'block' your website/webapp. All other events (e.g. the user clicks on a button) will continue to work, even during a BANano.Sleep.

    Alain
     
    Last edited: Feb 5, 2019
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice