1
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? 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;

    },
RokasPo
  • 53
  • 6
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – VLAZ Nov 28 '19 at 19:29
  • you are missing a semicolon ; at var answer; – Gustavo A Garcia Nov 28 '19 at 19:33
  • @GustavoAGarcia [the semicolon is optional](https://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi) – VLAZ Nov 28 '19 at 19:35
  • I understand the basics of asynchronous functions, but I am having a hard time implementing this, I've tried going the call back route with creating variables for functions but I end up with the Object Promise problem and I got to a stuck really quick, maybe you can give me an example with my code. Sorry if I'm being needy... – RokasPo Nov 28 '19 at 19:38
  • Yeah forgot to add it, but It still doesn't fix the issue. :/ – RokasPo Nov 28 '19 at 19:39
  • maybe this will help you https://www.pluralsight.com/guides/javascript-callbacks-variable-scope-problem – Gustavo A Garcia Nov 28 '19 at 19:41
  • I will look into this link and try to understand it – RokasPo Nov 28 '19 at 19:45

2 Answers2

1

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. I think it should work if I did not miscalculated brackets ;)

dismedia
  • 129
  • 7
  • Oh, That really clears things up, but why in the html you put `App.ShowInfo(i).then(function(ans)` when the ans is now not defined? – RokasPo Nov 28 '19 at 21:16
  • ans should be defined. if its not .. I added console.log to clarify if we getting something from [instance] – dismedia Nov 28 '19 at 21:26
0

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.

dismedia
  • 129
  • 7
  • My intention is just to return the thing i get from the line: instance.showFName(number); I get the same number from the Cert variable, but I guess that's just unnecessary code – RokasPo Nov 28 '19 at 19:40
  • Understood. But you, simply sayn, can not do that. You need to use promise as a "Carrier" of a value that you dont have yet. You dont have it because procces of getting that value is async. That why you need to return Promise – dismedia Nov 28 '19 at 19:43
  • Okay I'm starting to get what you mean, but my biggest problem is that I don't understand how to "unbox" the "carrier" I guess – RokasPo Nov 28 '19 at 19:47
  • You unboxin it with : then( function(value) {...}) – dismedia Nov 28 '19 at 19:48
  • At the place you need to condume (use) this value. So probably outsude this function. – dismedia Nov 28 '19 at 19:49
  • I have edited the other function for you too see what I'm doing wrong, but I am going to look into the async callbacks further. – RokasPo Nov 28 '19 at 20:03