roblox-aurora / rbx-net

Advanced multi-language networking framework for Roblox
https://rbxnet.australis.dev/
MIT License
93 stars 12 forks source link

How to wait for a Promise in a RemoteFunction callback? #11

Closed Gskartwii closed 5 years ago

Gskartwii commented 5 years ago

Background info: I'm using rbx-net with roblox-ts to create a turn-based game. A network design pattern that feels idiomatic for the game is to have a RemoteFunction named "AskPlay" that waits for the player to give input and then resolves a Promise. On the server, this is easy to do, as CallPlayerAsync returns a Promise.

However, I can't set the client callback to be an async function, as async functions return a Promise that rbx-net doesn't wait for to be resolved. As a result, I can't await for the player to make their choice. So ideally, I would like my code to look like this:

        askPlay.setCallback(async () => {
            let localPlayer = state.LocalPlayer()
            let playedCards = await this.AskPlay()

            localPlayer.Hand!.RemoveCards(playedCards.Cards)

            return playedCards.Cards.map((card) => localPlayer.Hand!.Cards.indexOf(card))
        })

Here this.AskPlay() returns a Promise that is resolved upon user input. Could it be that this design isn't in line with how rbx-net is designed to work?

Vorlias commented 5 years ago

Should not really be using CallPlayerAsync, here's a good explanation why.

https://www.youtube.com/watch?v=0H_xcA-0LDE

It will also be removed in a future release of rbx-net.

Vorlias commented 5 years ago

You should now be using NetServerAsyncFunction and NetClientAsyncFunction for this.

I will look into having it properly deal with async functions.