Encrypt data with RSA in Javascript synchronously

I need to encrypt user-entered data (username, email and password) using Javascript to not transfer it over the network in the plain text. (I do using HTTPS, but RSA is here just for test purposes, I want to replace it with McEliece once in the future…) I have written the following code:

        function str2ab(str)
        {
            const buf = new ArrayBuffer(str.length);
            const bufView = new Uint8Array(buf);
            for (let i = 0, strLen = str.length; i < strLen; i++)
                bufView[i] = str.charCodeAt(i);
            return buf;
        }
        function importKey(pem)
        {
            const pemHeader = "-----BEGIN PUBLIC KEY-----";
            const pemFooter = "-----END PUBLIC KEY-----";
            const pemContents = pem.substring(
                pemHeader.length,
                pem.length - pemFooter.length - 1,
            );
            const binaryDerString = window.atob(pemContents);
            const binaryDer = str2ab(binaryDerString);
            return window.crypto.subtle.importKey(
                "spki",
                binaryDer,
                {
                    name: "RSA-OAEP",
                    hash: "SHA-512",
                },
                true,
                ["encrypt"],
            );
        }
        var pubkey = importKey(`
-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----
`);
        alert(pubkey); //Shows "[object Promise]"
        var encrypted = window.crypto.subtle.encrypt(
            {
                name: "RSA-OAEP",
            },
            pubkey,
            JSON.stringify(...),
        ); //TypeError: SubtleCrypto.encrypt: Argument 2 does not implement interface CryptoKey.
        ...

If I replace the importKey() function with the following:

        function importKey(pem)
        {
            const pemHeader = "-----BEGIN PUBLIC KEY-----";
            const pemFooter = "-----END PUBLIC KEY-----";
            const pemContents = pem.substring(
                pemHeader.length,
                pem.length - pemFooter.length - 1,
            );
            const binaryDerString = window.atob(pemContents);
            const binaryDer = str2ab(binaryDerString);
            let result;
            window.crypto.subtle.importKey(
                "spki",
                binaryDer,
                {
                    name: "RSA-OAEP",
                    hash: "SHA-512",
                },
                true,
                ["encrypt"],
            ).then(key => result = key);
            return result;
        }
  • alert shows “undefined”. Unfortunately, I could not use async calls since the function where I try to encrypt is synchronous and this cannot be changed. How to call encryption method synchronously? I do want to freeze a page, since I hope it won’t be a long time. I know there is .Result property in C# (my main language), maybe there is something similar in Javascript?
    I don’t use packs like Node.js etc., so the methods described here don’t work.