As the title says... Chat on...

User avatar
By timoline
#13665 If I execute the following directly in esplorer, the aaps var will be nil,
if I check the aaps var a second later it has the value... :shock:

Why is it nil the first time I execute the code???
I tried to delay it, but that doesnt work....(or maybe i did it wrong)...

snip
Code: Select allend
> wifi.setmode(wifi.STATION)
> print("Get available APs...")
Get available APs...wifi.sta.getap(listap)
> print(aaps)
nil
> print(aaps)
<option value=.........................................etc'


the code
Code: Select allfunction trim(s)
    return (s:gsub("^%s*(.-)%s*$", "%1"))
end

function listap(t)
    aaps = ""
    if t then
        for k,v in pairs(t) do         
            ap = string.format("%-10s",k)
            ap = trim(ap)
            aaps = aaps .. "<option value='".. ap .."'>".. ap .."</option>"           
        end
    end
end

wifi.setmode(wifi.STATION)
print("Get available APs...")
wifi.sta.getap(listap)

print(aaps)
User avatar
By draco
#13754 listap() is a callback function.

The way a callback function works is:
    1. You call the function you are interested in, for example, wifi.sta.getap(), and give it the name of your callback function.
    2. wifi.sta.getap() goes out and runs, which can take a few seconds.
    3. Your callback function then gets run with the results.
So, if you try to read "aaps" right away, you get no result, because step 2 there is still in progress. your callback function has not yet been run.
When you wait a second and check again, steps 2 and 3 have had time to complete, so you see your results.
User avatar
By timoline
#13783
draco wrote:listap() is a callback function.

The way a callback function works is:
    1. You call the function you are interested in, for example, wifi.sta.getap(), and give it the name of your callback function.
    2. wifi.sta.getap() goes out and runs, which can take a few seconds.
    3. Your callback function then gets run with the results.
So, if you try to read "aaps" right away, you get no result, because step 2 there is still in progress. your callback function has not yet been run.
When you wait a second and check again, steps 2 and 3 have had time to complete, so you see your results.


thx for your answers, I think I understand,
but:
4. how to put the result in a var
5. use that var with result later in the code

cant be that hard for a newbie....or...... :oops: