我在合约MyContract
中有一个非常简单的可靠性函数。
function getCount() view public returns (uint) {
return myArray.length;
}
以下使用web3
的javascript会打印[object Promise]
而不是计数?
MyContract.deployed().then(i => {
var count = i.getCount.call();
console.log(count); // print [object Promise]
})
答案 0 :(得分:1)
根据您的代码:
MyContract.deployed().then(i => {
var count = i.getCount.call();
console.log(count); // print [object Promise]
})
MyContract.deployed()将合同部署到eth网络。矿工需要时间来验证并将合同代码添加到区块链。成功完成此过程后,()将调用。
来到i是部署合同对象,使用i variable
您可以访问合同。
i.getCount.call().then(val =>{ console.log("Value")}) //instance.getCount().call will returns promise. So
call()是异步方法,即它不会等待完成步骤。当你获得那时的数据时,()将调用。
或者只需致电instance.getCount()
您的执行将暂停,直到您获得结果。
我的选择是使用then()