Get Public key variable from Freighter Wallet

Get Public key variable from Freighter Wallet

Manage alerts

Loading saved threads...

nocodestellar · External communityPost link
External question — Stellar Stack Exchange Author: nocodestellar Original post: https://stellar.stackexchange.com/questions/5916 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. if (window.freighterApi.isConnected()) { alert("User has Freighter!"); } const retrievePublicKey = async () => { let publicKey = ""; let error = ""; try { publicKey = await window.freighterApi.getPublicKey(); } catch (e) { error = e; } if (error) { return error; } return publicKey; }; const result = retrievePublicKey(); Hi. This is Freighter Wallet javascript code. I try to find a way to get publicKey outside of this code. when I console.log(publicKey) after "await" everything ok. But I can't set publicKey to other variables to use outside of this code. When I set a variable after "await" and use it outside it is just undefined. result log gives me "promise pending". I can use "then" to get results on "console.log" . but still don't know any way to set the result to a variable to use.
Quote
Report
Glorfindel · External communityPost link
External answer — Stellar Stack Exchange Author: Glorfindel Original post: https://stellar.stackexchange.com/a/5917 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. You can define a global variable at the top: var publicKey; (remove the line let publicKey = ""; ) and set it like you do already: publicKey = await window.freighterApi.getPublicKey(); But it will still be available only after the retrievePublicKey has run; it's an async function so you have to await it, and that cannot be done from the 'main' method (just await retrievePublicKey() won't work). You can do it with then , as you already tried, or a self-invoked async function as described in this Stack Overflow answer .
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External question — Stellar Stack Exchange Author: nocodestellar Source score (net votes, not local likes): 2 Original post: https://stellar.stackexchange.com/questions/5916 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. if (window.freighterApi.isConnected()) { alert("User has Freighter!"); } const retrievePublicKey = async () => { let publicKey = ""; let error = ""; try { publicKey = await window.freighterApi.getPublicKey(); } catch (e) { error = e; } if (error) { return error; } return publicKey; }; const result = retrievePublicKey(); Hi. This is Freighter Wallet javascript code. I try to find a way to get publicKey outside of this code. when I console.log(publicKey) after "await" everything ok. But I can't set publicKey to other variables to use outside of this code. When I set a variable after "await" and use it outside it is just undefined. result log gives me "promise pending". I can use "then" to get results on "console.log" . but still don't know any way to set the result to a variable to use.

Cancel quote

Checking account access…