I'm designing an HTML file (or a user interface: UI) for my smart contract.

let's assume that my contract has a state as follows:

address public owner;// it is set to "0x5574Da45Ce5115055BB46c668D9982868ABd1382" when the contract's deployed

As we all know, to get the value of "owner" we can use the following command line in "geth":

myContract.owner.call({from: "0xEE42224D91492247da80724CAD937C2273f1D5ed"})

But the above code does not work if I use it in my HTML file (I get an error mentioned here). It should have the following form to let Metamast work with it (might be other way too, but I'm not aware of it):

myContract.owner.call({from: "0xEE42224D91492247da80724CAD937C2273f1D5ed"}, function(err, result) { if(!err) 
// do something

Apparantly the value allocated to "owner" is stored in "result". But I don't know how to get it and print it on my HTML page!

Question: How to get/store the value returned by the above callback function?

Note that we can use console.log(result) in the above function but, in this way, the result does not still appear on the webpage and it appears in the background.

closed as off-topic by smarx, Ismael, shane, Briomkez, Richard Horrocks Jan 18 at 9:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Ethereum, the decentralized application platform and smart contract enabled blockchain, within the scope defined in the help center." – smarx, Ismael, shane, Briomkez, Richard Horrocks
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Try this comment, they used an example to show an async function returning a value. reddit.com/r/ethdev/comments/8dyfyr/… – Fortune Jan 17 at 19:30
  • 1
    This question doesn't belong here. It's about JavaScript, callbacks, and how to modify the HTML DOM. Nothing relates to Ethereum except that the value in question happens to come from a smart contract. – smarx Jan 17 at 20:44

This isn't exactly a MetaMask issue. The keyword to search for is "callback" in JavaScript.

You can't fetch the value or assign it syncronously, e.g.

var balance = contract.function.call(...

It has to be async, which means the response is expected later. This is why the second code example passes a function into the call. The function passed in is to run when the response comes back.

It's a big topic and I want to keep this succinct, hence the "keyword" hint.

Hope it helps.

  • thanks for the answer and the hint. Then how the result of a smart contract call can be shown on a webpage? How is it done in the "remix" or "myEtherWallet" for instance? – Ay. Jan 17 at 19:13
  • It's a big rabbit hole that will go beyond this comment format. The result gets passed into the callback function that was passed into the original function. The dialog is roughly "Do this, and when you're done, send the answer to this function." You see the function receives a result, then goes on to do something with the result. – Rob Hitchens - B9lab Jan 17 at 20:38
  • If you haven't seen callbacks (and promises) before, expect to spend a day or so getting the hang of it. – Rob Hitchens - B9lab Jan 17 at 20:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.