I started experimenting with Promises and I don’t know how to wait for Promise to resolve or reject.
I tried
promise:await(function(_, result)
print(result)
end)
but it doesn’t print anything.
I started experimenting with Promises and I don’t know how to wait for Promise to resolve or reject.
I tried
promise:await(function(_, result)
print(result)
end)
but it doesn’t print anything.
Await is the correct way to wait for a Promise to complete. You need to show more code than this, specifically the Promise you’re awaiting on.
This is the module script with functions that use promises:
And this is the script where I get the result:
You don’t need to pcall inside a Promise since errors during execution are translated into rejections:
return Promise.new(function (resolve)
resolve(player1:IsFriendsWith(player2))
end)
In any case though, await is a method call that returns the results of the Promise so you’re not using it correctly here. What you want instead is to assign the returns to variables:
local resolved, result = promises:IsFriendsWith(Players.LocalPlayer, player.UserId):await()
andThen is what you’re looking for if you want to chain a result handler onto the Promise since your await right now is currently being used like andThen.
promises:IsFriendsWith(Players.LocalPlayer, player.UserId):andThen(function (result)
local append = if result == true then "friends" else "not friends"
print("Players are " .. append)
end)
Side note: make sure to :catch() rejections so you can handle them in your code! You can still chain Promises off of a catch which itself returns a Promise.
This seems to be working, thank you! Don’t even know why I though that it should be like this (probably because how :andThen is working).
Actually, I have a chain of these functions and they are made for my custom player list to get icons next to the player’s username. In player list these icons should have a priority, so thats why I don’t use :andThen.