Javascript Can';t从NodeJS中的promise更新全局变量

Javascript Can';t从NodeJS中的promise更新全局变量,javascript,node.js,web3js,Javascript,Node.js,Web3js,我是Nodejs新手,我一直在为我的合同编写一个Web3处理程序,一切都很好,但是全局变量在承诺执行期间不会更新,即使在承诺执行之后我使用console.log,它们也没有任何价值 ... var cityDb = {}; function gettingBalance() { return new Promise ( (resolve, reject) => { Contract.totalSupply.call(function(error, result

我是Nodejs新手,我一直在为我的合同编写一个Web3处理程序,一切都很好,但是全局变量在承诺执行期间不会更新,即使在承诺执行之后我使用console.log,它们也没有任何价值

...
var cityDb = {};
function gettingBalance() {

    return new Promise ( (resolve, reject) => { 

        Contract.totalSupply.call(function(error, result){
            if(!error)
                {

                resolve(result.toNumber());
                }
            else

                reject(error);
        });
    });
}


function gettingCities(cityNumber) {

    return new Promise ( (resolve,reject) => { 

        Contract.CityDB.call( cityNumber, (error, result) => {
            if(!error)
                {

                    resolve(result);

                }
            else
                reject(error);
        });

    });

}



gettingBalance()
    .then ( (result) => {
        console.log('Total after function: '+result);
        return result;
    })
        .then ( (totalSupply) => {
            console.log('loop data :'+totalSupply);

            // START SECOND PROMISE 

            for (let i=0; i <= totalSupply; i++) { 

                gettingCities(i) 
                    .then( (result) => {

                    cityDb[i] = {
                            cityName: result[0],
                            Population: result[1],
                            };


                        let j = i-1;
                        console.log('Loop ['+i+']'+ cityDb[i].cityName);

                    })
                    .catch( (error) => {
                        console.log(error);
                    });

                    if (i == totalSupply) { return;  }

            }


            // END SECOND PROMISE

        })
        .then ( (response) => {
            console.log('Test promise loop: '+ cityDb[2]);
        })
        .catch( (err) => {
            console.log('loop error: '+err);
        });

为什么测试承诺循环没有定义?cityDB没有从循环中获得更新,但循环工作正常。

问题在于没有正确链接承诺。您上一次的
then
没有等到所有
gettingCities
调用完成。为此,您可以使用
Promise.all

gettingBalance()
    .then((result) => {
        console.log('Total after function: ' + result);
        return result;
    })
    .then((totalSupply) => {
        console.log('loop data :' + totalSupply);

        // START SECOND PROMISE 
        const promises = [];

        for (let i = 0; i < totalSupply; i++) {
            // We push gettingCities promises into the array
            promises.push(gettingCities(i));    
        }

        // We pass gettingCities promises into Promise.all
        return Promise.all(promises);

    })
    .then((results) => {

        // All gettingCities results
        // We loop through each result and fill cityDb
        results.forEach((result, i) => {

            cityDb[i] = {
                cityName: result[0],
                Population: result[1],
            };

            console.log('Loop [' + i + ']' + cityDb[i].cityName);
        })

        console.log('Test promise loop: ' + cityDb[2]);
    })
    .catch((err) => {
        console.log('loop error: ' + err);
    });

我通过使用async/await…修复了我的问题。。。。我仍然很好奇为什么变量在“then”中使用时没有赋值,所以如果有人回答我,我将不胜感激。检查我的答案,并让我知道它是否有用。谢谢你的回答!所以答应我,我所需要的就是。。。还不是很清楚,我得试试。您只能在Promise.all中传递承诺,但您传递了一个对象。另外,你似乎用了推力来推动一个物体,它能起作用吗?谢谢,不客气<代码>承诺。所有都接受一个承诺数组,我传递了一个数组,记住,
[]
是一个数组,一个对象文本是
{}
。没问题,我们有时都是新人:)!很乐意帮忙。
gettingBalance()
    .then((result) => {
        console.log('Total after function: ' + result);
        return result;
    })
    .then((totalSupply) => {
        console.log('loop data :' + totalSupply);

        // START SECOND PROMISE 
        const promises = [];

        for (let i = 0; i < totalSupply; i++) {
            // We push gettingCities promises into the array
            promises.push(gettingCities(i));    
        }

        // We pass gettingCities promises into Promise.all
        return Promise.all(promises);

    })
    .then((results) => {

        // All gettingCities results
        // We loop through each result and fill cityDb
        results.forEach((result, i) => {

            cityDb[i] = {
                cityName: result[0],
                Population: result[1],
            };

            console.log('Loop [' + i + ']' + cityDb[i].cityName);
        })

        console.log('Test promise loop: ' + cityDb[2]);
    })
    .catch((err) => {
        console.log('loop error: ' + err);
    });
.then((results) => {

    // gettingCities results
    const cityDb = {};
    results.forEach((result, i) => {

        cityDb[i] = {
            cityName: result[0],
            Population: result[1],
        };

        console.log('Loop [' + i + ']' + cityDb[i].cityName);
    })

    console.log('Test promise loop: ' + cityDb[2]);
})