How to get result value out of async await function?
I am trying to get current chain ID in the MetaMask, I get object in return of the function.
I am expecting 0x4
, but it is not accessible outside of the function.
let account;
let currentChain;
const switchNetwork = async () => {
currentChain = await ethereum.request({ method: 'eth_chainId' });
console.log(currentChain + ' <- currentChain'); //for debug
return currentChain; //tried
}
let fromCheck = switchNetwork();
console.log(fromCheck + ' <- fromCheck'); //for debug, expecting `0x4`
Result:
[object Promise] <- fromCheck
0x4 <- currentChain
Object looks like this:
Promise {<pending>}[[Prototype]]: Promise[[PromiseState]]: "fulfilled"[[PromiseResult]]: "0x4"
0x4 <- currentChain