Unfortunately, you were just lucky that the answer came during the 10ms of your setTimeout.
When you enter the while-loop nothing else will run in the same QA.
In fact, nothing happens in parallel in a QA, not call-backs, not calls to QuickApp: function etc. You need to constantly give time to other parts of your code with setTimeout.
My example works because it's between two different QAs.
If you use it within the same QA you need to use callbacks and setTimeout loop - not as elegant / simple.
Ex. (Server same)
Customer:
local function rpc(callback,id,timeout,fun,...)
local v,args = "RPC"..tostring({}):sub(10),{...}
api.post("/globalVariables",{name=v})
setTimeout(function() fibaro.call(id,"RPC",{id=plugin.mainDeviceId,var=v,fun=fun,args=args}) end,0)
local t,res=os.time()+timeout
local function loop()
if os.time()<=t then
local res = fibaro.getGlobalVariable(v)
if res and res~= "" then
api.delete("/globalVariables/"..v)
res = json.decode(res)
if res[1] then
callback(select(2,table.unpack(res)))
else print("RPC error:"..res[2]) end
else setTimeout(loop,10) end
else
api.delete("/globalVariables/"..v)
print("RPC Timeout:"..fun)
end
end
loop()
end
local function rpcFun(id,timeout,fun) return function(cb,...) return rpc(cb,id,timeout,fun,...) end end
function QuickApp:onInit()
self:debug("onInit ",plugin.mainDeviceId)
local foo = rpcFun(240,5,"foo")
for i=1,10 do foo(print,8,i) end
end