So I’ve seen a lot of things like GetAsync
, SetAsync
, UpdateAsync
, GetFriendsAsync
, UserHasBadgeAsync
, but I don’t actually get what does Async
mean.
What does it mean?
So I’ve seen a lot of things like GetAsync
, SetAsync
, UpdateAsync
, GetFriendsAsync
, UserHasBadgeAsync
, but I don’t actually get what does Async
mean.
What does it mean?
“Async” means asynchronous
these functions let Roblox servers handle the work while your script waits for a response
aka wouldnt put heavy load on you for calling it
So I would explain like this:
For example the UserHasBadgeAsync
would return true or false depending if the player has the badge or not.
It returns a promise.
Imagine your mom was making toast, and she needs you to get butter from the supermarket.
Making a trip to the supermarket isn’t instantaneous, and a lot of things may go wrong - your car may blow up, or that shop simply doesn’t sell butter.
Your mom can’t make toast without it either, so all she can do is wait for you to be back with the butter.
That’s a nutshell of what async does.
For example, GetAsync
starts a new thread (“script” in air quotes) to get whatever data you left in the datastores, and halts the calling thread until there’s a valid response, or if it fails.
In this case, your mother would be the calling thread here trying to get something from a datastore.
You are the new thread that’s actually fetching data from storage, and asking your mom to wait until you are back.
There are a few other cases where the calling thread doesn’t halt itself (most TextService methods are like this).
The essence is the same - start a new thread that gets something, then halt the calling thread until it returns that something.
Doesn’t asynchronous functions not yield the thread that called it and instead work in the background, returning a Promise or some sort when they’re finished.
It should be called GetSync
tbh.
An async method or function doesn’t necessarily mean the method has to NOT yield itself to be counted as one.
If it even spawns an extra thread to do its thing, that is technically already asynchronous- you are still doing multiple things at once, just that one has to wait for the other.
The reason it has to be done this way is because datastore calls (and the like) are all HTTP - it’s safer and makes more sense to wrap that relevant code in a separate thread.
I think the reason is because it’s asynchronous for Roblox, not for your code. Your code is yielded, but the engine itself isn’t, so it can keep rendering and performing physics calculations and running other scripts.
Asynchronous code is basically code that runs at the same time as another piece of code, which lets you multitask.
Take a Promise, for example. It sets off code to execute and adds callbacks to execute when it’s finished.
//i used javascript because it's the first and simpler one i thought of sorry
async function foo() {
//run code here, blah blah blah.
return 3
};
foo().then(data => console.log(data)).catch(error => console.log(error));
/*
.then will execute when foo has finished running. So, when foo finishes,
it returns 3 which is then put to the output.
.catch will catch errors, but that's not entirely relevant here.
*/
The key thing to note is it doesn’t cause your code to yield or wait for a response.
local function foo()
--code goes here etc, etc.
end
print("start")
foo() --code pauses here and waits for foo to finish executing
print("end")
In synchronous code, the thread will go:
But, if we made foo
asynchronous:
You could think of it this way:
imagine you’re cooking a meal. You turn on your oven, but you don’t stand there and wait for it to warm up before you start preparing food. You prepare food in the meantime. Then, when the oven is ready, you put the food in it and cook it. You’ve started an asynchronous task, preparing the food, and scheduled a callback to be run when the promise is resolved - put the food in the oven.
It’s important to note Lua doesn’t support asynchronous code. It does a clever trick where it splits each task up into tiny chunks and runs parts of those chunks in turn with each other to appear that code is running asynchronously.
In terms of data store operations, I’ll introduce you to another thing - waiting for async operations to complete.
//take my code snippet from earlier. We can wait for this.
async function foo() {
//code
return 3
};
//note for javascript enthusiasts: i know await could not be used here synchronously. for the purpose of this example, imagine this thread is asynchronous.
let result = await foo();
console.log(result);
here, our code waits for the operation to happen, the same way in the kitchen you could wait for the oven to heat up.
note: this info may not be entirely correct, but I’m pretty sure it is!
GetAsync
Basically what i said before, when we call it the Roblox C++ code will schedule our code to after the operation is complete. The code itself is asynchronous, but our code is actually scheduled on the .then
branch of the promise, you could say. It uses await to wait for the asynchronous code to finish.
GetAsync().then(data => ourDataStoreCode).catch(error => /* our code on the error side of pcall */)
await GetAsync()
This is the same with our SetAsync
code, except the request is only added to the queue. It’s then added to a separate queue of asynchronous operations and run to the store.
Did you ever hear you need to wait a while when the game server closes so the data requests have time to go through? Well, that’s why. The requests don’t actually get processed like that. They only get added to the queue. They’ll later be processed to the DataStore.
UpdateAsync().then(response => ourCodeThatFollows).catch(error => /*error branch of pcall*/)
DataStore:UpdateAsync(key, function(old)
end)
--here, our code is yielding via await so the code waits until the request is added to queue. The truly asynchronous nature happens after that, once our request is in the queue. It's scheduled to be written to the store.
Sorry this is quite a long reply, but I hope it helps!
I used javascript because it’s like the most asynchronous supportive language i know (yes i also know its single threaded for javascriot enthusiasts) sorry
roblox code is in C++ not javascript
But Luau in Roblox does, if anybody wants to know
Thanks for pointing that out, I wasn’t specific enough. I was talking about coroutines in one script, but yes, Parallel Luau does allow for “truly” asynchronous code instead of mock-async.
Asynchronous in a programming context just means it’s output can occur at almost any time. For example, a datastore API call is async because it will fetch the data and will momentarily return it’s output. A synchronous function, on the other hand, gives you it’s output without any delays