B4J Tutorial [BANano] Resumeable Sub (sort of)

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:
B4X:
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:
B4X:
[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:
Top