performWithDelay doesn't seem to be working

I am trying to write a mock log in while developing my app so I don’t have to actually ping my servers. I wanted to delay the log in to make it look like it was waiting for a response from the server. For some reason, the M:logInUser function is returning immediately and doesn’t return the true/false like I was expecting.

Is there an error with the performWithDelay that is keeping it from delaying? Also, does the callback function not return the boolean? Any help would be greatly appreciated.

local function mockLogIn( email, pass )     for \_, user in ipairs(mockUsers.data) do         if (email == user.email and pass == user.password) then             return true         end     end     return false end function dataService:logInUser( email, pass )     return timer.performWithDelay( 2000, mockLogIn(email, pass)) end

In my calling function I am doing this, but it is printing nil:

local function checkLogin( email, password )     local temp = dataService:logInUser( email, password )     timer.performWithDelay( 4000, print(temp) ) end

When you pass a function call in a timer, it is executed immediately at runtime.  You have to pass in a listener instead.  The listener is called after the specified delay.  See the bottom of this page for an example of how to do what you need

To kinda add on to Jon’s answer.

When you call timer.performWithDelay( delayTime, functionToCall) and you do something like this;

timer.performWithDelay( 1000,  doSomething()  ) -- note the parens after "doSomething".

In your case when you pass in:   print(temp)

the value of temp will be printed immediately. The print function doesn’t return any thing. So in effect, you are passing nil to timer.performWithDelay and in 4 seconds it will do absolutely nothing.

It executes doSomething() and takes whatever it returns and uses that as the address to function that the timer will run in 1000 milliseconds, however, if you do:

timer.performWithDelay( 1000,  doSomething  ) -- note there are no parens on doSomething.

In this case, the address of the function doSomething is passed in and that’s what timer.performWithDelay is expecting. It wants and address to a function.

Also, the timer.performWithDelay() function returns a handle to the timer object. Your return timer.performWithDelay( 2000, mockLogIn(email, pass)) will not return the value of mockLogin(). You can’t return values this way either.

When you pass a function call in a timer, it is executed immediately at runtime.  You have to pass in a listener instead.  The listener is called after the specified delay.  See the bottom of this page for an example of how to do what you need

To kinda add on to Jon’s answer.

When you call timer.performWithDelay( delayTime, functionToCall) and you do something like this;

timer.performWithDelay( 1000,  doSomething()  ) -- note the parens after "doSomething".

In your case when you pass in:   print(temp)

the value of temp will be printed immediately. The print function doesn’t return any thing. So in effect, you are passing nil to timer.performWithDelay and in 4 seconds it will do absolutely nothing.

It executes doSomething() and takes whatever it returns and uses that as the address to function that the timer will run in 1000 milliseconds, however, if you do:

timer.performWithDelay( 1000,  doSomething  ) -- note there are no parens on doSomething.

In this case, the address of the function doSomething is passed in and that’s what timer.performWithDelay is expecting. It wants and address to a function.

Also, the timer.performWithDelay() function returns a handle to the timer object. Your return timer.performWithDelay( 2000, mockLogIn(email, pass)) will not return the value of mockLogin(). You can’t return values this way either.