When & Why should I use promises?

Hello DevForum,

as can be read by my title, I am not sure why I should use promises.
I get that they represent a value that will be there in the future, but I don’t see why I wouldn’t yield instead.

For instance, if for some reason I send an HTTP request each time a player joins:

Code
local HttpService = game:GetService("HttpService")
game:GetService("Players").PlayerAdded:Connect(function(client)
	local ok, response = pcall(HttpService.RequestAsync, HttpService, {Url = "someUrl"});
	if (ok) then
		-- do stuff
	else
		-- handle error
	end
end)

Should I use a promise instead of yielding there?
And if so, why would I use a promise if events are async, it wouldn’t stop any other code from executing.
Adding onto that, should I replace every piece of code I have which yields (Like :WaitForChild()) with a promise instead?

I’ve read posts about promises like evaera’s but I still don’t understand the usage of promises.

Appreciate all the help in advance, I am quite confused about this.
Cheers.

1 Like

Coming from someone who uses JS a lot, promises are basically one of the more reliable ways of yielding for a value without stopping the thread. (I’m not an expert by any means have someone else really explain it to you)

Depending on the type of programming you’re doing, a promise may not be for you. Promises are a type of thing that you use for a value that may or may not be coming. It’s just going to promise you you’ll get the result. This way you can actually cancel the promise if you don’t need it, or you can handle the error immediately.


Depending on what you’re doing there. If you want to run code elsewhere in that event while that’s happening go ahead, but you really don’t need to if you don’t want to.


Your choice. I wouldn’t seeing as I am comfortable just using Roblox’s yielding (although I know it’s not reliable) If you have it all functional don’t tear it down.


Promises are very useful in JS but you don’t have to use them just because they’re useful elsewhere.

2 Likes