简体   繁体   English

Javascript 中的全局变量以及如何从 then() JavaScript 函数返回变量

[英]Global variables in Javascript and how to return variables from then() JavaScript function

ShowInfo : function (number) {

        var answer
        App.contracts.StudentState.deployed().then(function (instance) {
            return instance.showFName(number);
        }).then(function (Cert) {
            answer = Cert;
        })

        console.log(answer);
        return answer;

    },

Here is the function I have been trying to perfect for too much time then I should.这是我一直试图完善的功能,我应该这样做。 I am new to JavaScript and need for this function to return a variable called answer, but I always get it as undefined, I know that in JavaScript I just can't have global variables that easily, but how do I fix this code?我是 JavaScript 的新手,需要此函数返回一个名为 answer 的变量,但我总是将其设为未定义,我知道在 JavaScript 中我不能那么容易地拥有全局变量,但是如何修复此代码? This is linked with Ethereum smart contracts from where I receive a number.这与我收到一个号码的以太坊智能合约相关联。

Thank you for your time and effort.感谢您的时间和精力。

Well these are the two code lines I'm using at the moment:好吧,这是我目前正在使用的两行代码:

var wrapper = document.getElementById("myHTMLWrapper");

    var myHTML = '';
    for (var i = 0; i < num; i++) {
        var ans =  App.ShowInfo(i);
        myHTML += '<span class="test">INFO:' + ans + '</span><br/><br/>';
    }

    wrapper.innerHTML = myHTML
ShowInfo : function (number) {

        var answer = App.contracts.StudentState.deployed().then(function (instance) {
            return instance.showFName(number);
        })

        console.log(answer);
        return answer;

    },

Now I see the big picture :)现在我看到了大图:)

so..所以..

ShowInfo : function (number) {

        var answer = App.contracts.StudentState.deployed().then(function (instance) {

     console.log(instance.showFName(number)) //this should be in [ans] later
     return instance.showFName(number); 
     //assuming that showFName returns string, not another promise

     })


        return answer; //this is still a Promise

    },

then..然后..

var wrapper = document.getElementById("myHTMLWrapper");

    for (var i = 0; i < num; i++) {
        App.ShowInfo(i).then(function(ans){
        wrapper.innerHTML+='<span class="test">INFO:' + ans+' ('+i.toString()  + ')</span><br/><br/>';
      })

    }

Now I see that you dealing with multiple async actions what complicates think a little bit..现在我看到你在处理多个异步操作,这让我觉得有点复杂。

if you dealing with consuming multiple promises you need to decide if you want to act (in your case display result) when any of them resolves (finishes), or maybe all of them are done.如果您处理使用多个承诺,您需要决定是否要在其中任何一个解决(完成)时采取行动(在您的情况下显示结果),或者可能所有承诺都已完成。

I choose to update your html at every moment when particular info is resolved.我选择在解决特定信息时随时更新您的 html。 I think it should work if I did not miscalculated brackets ;)如果我没有错误计算括号,我认为它应该可以工作;)

You need to return whole promise instead of answer because your action is async.您需要返回整个承诺而不是回答,因为您的操作是异步的。 At the moment you are returning answer it is undefined.目前您正在返回答案是未定义的。 So.. As far as I understand your intentions you need to return whole App.contracts.Stude.... chain.所以......据我了解你的意图,你需要返回整个 App.contracts.Stude .... 链。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM