Client code runs before Server Code

I’m making a IP ban system which uses client side (to get the users IP Address), and backend code (to see if the ban database contains the IP address).

I am able to get the client to send its IP address to the backend, and the backend is able to check if the IP address is listed as banned, but my issue is that the client side code finishes before the backend has finished.

Here’s the client side code:

    fetch('https://extreme-ip-lookup.com/json', {
            method: 'get'
        })
        .then((httpResponse) => {
            console.log("Got HTTP Response")
 if (httpResponse.ok) {
 return httpResponse.json();
            }
        })
        .then((json) => {

 let ipaddress = json.query;

 let banstatus = checkBan(ipaddress) // Returns "SERVER" message in console and sets banstatus as either "true" or "false"

 if (banstatus === "true") {
                    console.warn("CLIENT: IP is Banned")
 
                }
 else
                {
                    console.warn("CLIENT: IP is not Banned")
                }
        })


As you can see above in the console output, the Client logs that the IP is not banned before the Server has finished (or even started) it’s check.

It should be a simple fix, async or .then(), but I’m not sure on the best way to overcome it.

@hamza When you call backend code (from the frontend), a Promise is returned. Just make sure that you run your frontend code when the Promise is fulfilled. See the article Corvid: Calling Server-Side Code from the Front-End with Web Modules for more information.

You might also want to read the following articles on Promises: