簡體   English   中英

使用For Loop鏈接和嵌套的諾言

[英]Chained and Nested promises with For Loop

我試圖使我的游戲的每個屬性都處於鏈式承諾中(每個屬性都來自不同的異步調用)。

我的算法邏輯:

  1. 檢查網絡並獲取智能合約地址
  2. 注冊包含所有游戲地址的合同
  3. 獲取游戲數
  4. 對於每個游戲,每個媒體資源執行一次aSync通話
  5. 打印所有游戲和詳細信息(在這里我無法獲得更新的對象)

碼:

  var games = [];
  window.addEventListener('load', function() {
    // Check the Network and assign the smart contract address

    web3.eth.net.getId()
      .then(function(networkId) {
        let contractAddressRegistry;
        if (networkId == 1) {
          contractAddressRegistry = "0xQWERTYUIOPQWERTYUIOPQWERTY"
        } else {
          contractAddressRegistry = "0x12345678901234567890123456"
        }
        return contractAddressRegistry;
      })
      .then(function(contractAddressRegistry) {
        let contractRegistry = new web3.eth.Contract(contractAbiRegistry, contractAddressRegistry);

        contractRegistry.methods.numberOfGames().call()
          .then(function(numberOfGames) {

            for (let i = 0; i < numberOfGames; i++) {
              let game = {};
              game.propertyA = aSyncCallGetPropertyA(i); // Promise
              game.propertyB = aSyncCallGetPropertyB(i); // Promise
              game.propertyC = aSyncCallGetPropertyC(i); // Promise
            }
            games.push(game);
          })
      })
      .then(function() {
        console.log(games) // Empty
      })
  })

我嘗試使用Promises.all(),但由於在then()中有一些異步調用,因此我無法正確同步它。

如何確保讓對象游戲充滿其所有屬性?

您應該這樣使用Promise.all 基本上,您需要將所有三個aSyncCallGetProperty異步調用都包裝在Promise.all ,等待它們真正完成,然后將結果分配給對象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));
    })

@Lewis的代碼似乎正確,但我無法確定numberOfGames是什么。 假設它是您的問題中使用的整數(而不是其他答案中所使用的數組),那么這里是不帶嵌套的.then()的另一個改寫版本。

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);
                    });
  });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM