Hello everyone I need your quick help!
I am using web3 1.0.0-beta.34 with React and Next.js and I want to check if user has it Metamask set on mainnet/rinkeby network.
I have read quite a few solutions but I am having a slight problem implementing them.
Basically, I have a Layout component which serves as a wrapper around all the elements, so it is called on every page.
What I found so far is this:
web3.eth.net.getId().then(netId => {
switch (netId) {
case 1:
console.log('This is mainnet')
break
case 2:
console.log('This is the ropsten test network.')
break
case 3:
console.log('This is the kovantest network.')
break
case 4:
console.log('This is the rinkeby network.')
break
default:
console.log('This is an unknown network.')
}
})
I don't want to console.log it, I want to put it in some kind of a variable, but I am not able to use React state. To simplify it, all I want from this function is to return me a number/string ID of the network where the user is at the moment. For now, it is only returning me a Promise object, and I don't know how to extract the ID from it.
var net = web3.eth.net.getId();
console.log(net);
this is what it shows
Promise {<pending>}
__proto__:Promise
[[PromiseStatus]]:"resolved"
[[PromiseValue]]:1
What i really need is this [[Promise Value]] extracted to a const/var so that I can manipulate it.