ELI5 on Callbacks

I’m trying to understand what callback functions are, but any explanation I search for here or elsewhere either goes over my head or doesn’t apply to lua. Could somebody explain callbacks and asynchronous vs synchronous in very simple terms?

10 Likes

Callbacks are usually just functions called once some condition or something is completed. It’s generally better approach then waiting and creating more and more loops. Generally, at least in my case, callbacks are anonymous functions, as in, they are not defined somewhere specific, and sent as parameters.

One way they may be used is like so


local function processSomethingidk(somedata, callback)
  -- do something with somedata as an example
  local x = somedata + 1
  DataStore:SetAsync("123", x) -- Imagine a datastore call or something
  callback(x) --[[ Will be ran once above stuff are done, 
  so it "calls back" to the function to finish it. ]]
end

-- Example usage:
processSomethingidk(123, function(x)
  -- This will be ran once the callback is called
  print(x) -- Should output 124
end)

I personally use callbacks for my datastore system, that way i just do something along the lines of save(key, val, function(success, code) --[[do something here]] end)

19 Likes

Synchronous and asynchronous

Synchronous and asynchronous operations are pretty simple.

If you think about synchronised swimming, it relates to two people swimming along side each other. Asynchronous would mean that they are not in sync and are blocking each other.

In programming, this relates to the different tasks a script is doing.

If an operation is synchronous, it will be completed on a separate thread, allowing the rest of your code to continue. However, if an operation is asynchronous, it will stop any other code in that script from running after it until it’s done.

Callbacks

A callback, as said above, is basically a function which is called under certain circumstances, like if something is true. The most common place you will have seen them is like this:

players.PlayerAdded:Connect(function(p)
    print(p.Name)
end

In this case, the function is the callback, the action to be run.

The benefit of doing this is we don’t have to do an infinite loop to constantly check if there is a new player. We set it up and forget about it.

Summary

  • Syncronus tasks allow your script to continue while they are being executed
  • Asyncronus tasks stop your script from continuing until they are done with whatever they were doing.
  • Callbacks are just actions executed at a specific time

I hope I explained it simply enough. If not, just reply and I can clarify for you.

24 Likes

The above answers are both excellent, but if anyone is still looking for an ELI5 example here follows my best attempt at it.


Jimmy sells products to users. He gets requests from clients for different items. Jimmy does not know how to make the different items, but he knows a lot of factories that produce the different items that the clients want. Jimmy also has a friend Bob, who rides around in his van delivering the products to the clients.

Client Tom calls Jimmy and orders a chair for his home. Jimmy then calls Okay-A Furniture:tm: who will produce the chair. In code the procedure could look like this:

function OkayAFactory.Produce(furniture)
    local item = produce(furniture)
    return item
end

But there is an issue with this piece of code. This function returns item to the caller (Jimmy). However, if this item is sent back to Jimmy, then Jimmy has to send it to Bob and then Bob would have to send it to the client. That’s a lot of effort! What if we could instead tell Okay-A Furniture Factory what to do with the furniture after it is done? Fortunately, we can! This is what is called a callback, which is a function that is passed to a function/method/etc. which is then called at the end of the piece of code.

This is what the factory’s code could look like with a callback:

function OkayAFactory.Produce(furniture, deliverFunction)
    local item = produce(furniture)
    deliverFunction(item) -- replace the return with the callback!
end

Here is what Bob’s deliver function might look like:

function Bob.deliverFurniture(item)
    local address = item.Address
    Bob.TravelTo(address)
end

And then Jimmy would call the factory like this:

-- tell the factory to produce "chair" and pass it to Bob when it's done!
OkayAFactory.Produce("chair", Bob.deliverFurniture)

Now after the factory is done producing the chair, it will call back to Bob, because this is what Jimmy told them to do!

30 Likes

I think you have the two backwards. Synchronous is the one that blocks; asynchronous runs on a separate thread so the script can continue.

7 Likes

To confirm, did he have them backwards?

Yes. A synchronous operation blocks a process until the operation completes. An asynchronous operation is non-blocking. Which is confusing since “synchronous” literally means “at the same time”.

As far as programming goes, it probably means that an entire chunk of code is executed “at the same time” before moving on to something else, whereas asynchronous would mean that the processor is jumping back and forth between chunks (or handling them on different processors) and giving the appearance they they are happening concurrently.

[@CodeNinja16 plz msg or something so I remember to del this when you get a chance to flip those. Hopefully you don’t need a new metaphor. The mental imagery for that is great.]

5 Likes

Yep, sorry. I got it the wrong way around.

I thought it would be to do with synchronised means in-sync, but it seems not.

Thanks for the correction

A post was merged into an existing topic: Off-topic and bump posts