Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带有For循环的链接和嵌套承诺_Javascript_Asynchronous_Promise_Web3js - Fatal编程技术网

Javascript 带有For循环的链接和嵌套承诺

Javascript 带有For循环的链接和嵌套承诺,javascript,asynchronous,promise,web3js,Javascript,Asynchronous,Promise,Web3js,我试图在链式承诺中获得游戏的每个属性(每个属性都来自不同的异步调用) 我的算法逻辑: 检查网络并获取智能合约地址 注册包含所有奥运会地址的合同 得到游戏的数量 对于每个游戏,执行一个异步调用 每个属性 打印所有游戏和详细信息(此处我无法 要获取更新的对象,请执行以下操作: 代码: var游戏=[]; addEventListener('load',function()){ //检查网络并分配智能合约地址 web3.eth.net.getId() .then(函数(网络ID){ 让我们注册; if

我试图在链式承诺中获得游戏的每个属性(每个属性都来自不同的异步调用)

我的算法逻辑:

  • 检查网络并获取智能合约地址
  • 注册包含所有奥运会地址的合同
  • 得到游戏的数量
  • 对于每个游戏,执行一个异步调用 每个属性
  • 打印所有游戏和详细信息(此处我无法 要获取更新的对象,请执行以下操作:
  • 代码:

    var游戏=[];
    addEventListener('load',function()){
    //检查网络并分配智能合约地址
    web3.eth.net.getId()
    .then(函数(网络ID){
    让我们注册;
    if(networkId==1){
    contractAddressRegistry=“0xQWERTYUIOPQWERTYUIOPQWERTY”
    }否则{
    contractAddressRegistry=“0x12345678901234567890123456”
    }
    返回注册处;
    })
    .then(函数(contractAddressRegistry){
    让contractRegistry=newweb3.eth.Contract(ContractAbRegistry,contractAddressRegistry);
    contractRegistry.methods.numberOfGames().call()
    .然后(函数(numberOfGames){
    for(设i=0;i
    我尝试使用Promissions.all(),但无法正确同步,因为某些异步调用位于then()中


    如何确保对象游戏中包含所有属性?

    您应该像这样使用
    Promise.all
    。基本上,您需要将所有三个
    aSyncCallGetProperty
    async调用包装在
    Promise.all
    中,等待它们真正完成,然后将结果分配给object
    game

    whatever
        .then(function(contractAddressRegistry) {
            let contractRegistry = new web3.eth.Contract(contractAbiRegistry, contractAddressRegistry);
            return contractRegistry.methods.numberOfGames().call();
        })
        .then(function(numberOfGames) {
            return Promise.all(numberOfGames.map(() => {
                return Promise.all([
                    aSyncCallGetPropertyA(),
                    aSyncCallGetPropertyB(),
                    aSyncCallGetPropertyC()
                ]).then(results => {
                    let game = {};
                    game.propertyA = results[0];
                    game.propertyB = results[1];
                    game.propertyC = results[2];
                    return game;
                });
            }));
        })
        .then(function(games) {
            console.log(JSON.stringify(games));
        })
    

    @刘易斯的密码似乎正确,但我不能确定什么是
    numberOfGames
    。假设它是问题中使用的整数(而不是另一个答案中处理的数组),这里有一个没有嵌套
    .then()
    s的进一步改写版本


    aSyncCallGetProperty
    也会返回一个承诺,对吗?@Lewis:是的。编辑要更清晰。谢谢,您似乎正在调用
    then()
    窗口的返回值。addEventListener()
    。这是故意的吗
    addEventListener()
    不会返回承诺(或者任何东西)。@MátéSafranka真是个好主意!我把我的案子简化了一点。我加上了第一个错过的承诺电话。好吧,刘易斯抢先回答了我。另外,您可能还要检查一件事:它是
    contractRegistry.methods.numberOfGames().call()
    还是
    contractRegistry.methods.numberOfGames.call()
    ?不确定你的框架是如何工作的,只是看起来像是一个常见的打字错误。
    whatever
        .then(function(contractAddressRegistry) {
            let contractRegistry = new web3.eth.Contract(contractAbiRegistry, contractAddressRegistry);
            return contractRegistry.methods.numberOfGames().call();
        })
        .then(function(numberOfGames) {
            return Promise.all(numberOfGames.map(() => {
                return Promise.all([
                    aSyncCallGetPropertyA(),
                    aSyncCallGetPropertyB(),
                    aSyncCallGetPropertyC()
                ]).then(results => {
                    let game = {};
                    game.propertyA = results[0];
                    game.propertyB = results[1];
                    game.propertyC = results[2];
                    return game;
                });
            }));
        })
        .then(function(games) {
            console.log(JSON.stringify(games));
        })
    
    window.addEventListener('load', function() {
      web3.eth.net.getId()
                  .then(networkId => networkId === 1 ? "0xQWERTYUIOPQWERTYUIOPQWERTY"
                                                     : "0x12345678901234567890123456")
                  .then(contractAddressRegistry => new web3.eth.Contract(contractAbiRegistry, contractAddressRegistry).methods.numberOfGames().call())
                  .then(numberOfGames => Promise.all(Array(numberOfGames).fill()
                                                                         .map(_ => Promise.all([aSyncCallGetPropertyA(),
                                                                                                aSyncCallGetPropertyB(),
                                                                                                aSyncCallGetPropertyC()]))))
                  .then(function(games){
                          games = games.map(game => ({propertyA: game[0],
                                                      propertyB: game[1],
                                                      propertyC: game[2]}));
                          doSomethingWith(games);
                        });
      });