0
I have this function in JS, using Forge library, for generating pair of keys. The problem that when trying to use the async function to perform the generation, the function is returning before generating the result.
function generateKeys(keySize, storageName) {
var rsa = forge.pki.rsa;
// var keys = forge.pki.rsa.generateKeyPair(keySize);
var p1 = rsa.generateKeyPair({bits: keySize, workers: -1}, function(err, keypair) {
var privateKey = keypair.privateKey;
var publicKey = keypair.publicKey;
sessionStorage.setItem(storageName, JSON.stringify(privateKey));
var csr = forge.pki.createCertificationRequest();
csr.publicKey = publicKey;
csr.sign(privateKey);
// convert certification request to PEM-format
var certReqPem = forge.pki.certificationRequestToPem(csr);
console.log(certReqPem);
return certReqPem;
});
};
I get this return in my HTML to forward to a PHP form, however, it is coming NULL, but if I print it, I see that it sends Null, and then print it on the console
In HTML it looks like this: var publicKeyPem = generateKeys(keySize, "privateKeyPem"); When I do console.log on this variable, a promise in the pending state is coming.
– Vinicius Macelai
That’s why when being a prop, you have to run with async/await to make the execution of the promise awaiting its end async functions
– osiris85
Um, you say utilize . then? I’m trying, but without much success
– Vinicius Macelai
No, VC can do con I state in response, with async and await
– osiris85
I did as it is there, it is returning a promise, as said in the first reply
– Vinicius Macelai
In the first answer you don’t do
var publicKeyPem = await generateKeys(keySize, "privateKeyPem");
– osiris85
await da este erro: Syntaxerror: await is only Valid in async functions and async generators, que é bem estranho, pois a função está declarado como async
– Vinicius Macelai
Take the function to help
– osiris85
Ah got it, the problem I was calling it in another function in HTML, ai put async in it too, and await as said earlier, it worked, thanks.
– Vinicius Macelai