代码之家  ›  专栏  ›  技术社区  ›  ca9163d9

从javascript调用solidity函数获得[对象承诺]?

  •  1
  • ca9163d9  · 技术社区  · 7 年前

    我在合同中有一个非常简单的稳健性函数 MyContract .

    function getCount() view public returns (uint) {
        return myArray.length;
    }
    

    下面的javascript使用 web3 印刷品 [object Promise] 而不是计数?

    MyContract.deployed().then(i => {
      var count = i.getCount.call();
      console.log(count); // print [object Promise]
    })
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Jitendra Kumar. Balla    7 年前

    根据您的代码:

    MyContract.deployed().then(i => {
      var count = i.getCount.call();
      console.log(count); // print [object Promise]
    })
    

    我的合同。deployed()将合同部署到eth网络。矿工需要时间来验证并向区块链添加合同代码。成功完成此过程后,then()将调用。

    接下来是部署契约对象,使用 i variable 您可以访问合同。

    i.getCount.call().then(val =>{ console.log("Value")}) //instance.getCount().call will returns promise. So 
    

    call()是异步方法,即它不会等待完成该步骤。当您当时获得数据时,then()将调用。

    或者直接打电话 instance.getCount() 执行将暂停,直到得到结果。

    我的选项是use then()