1

I am trying to write a function, which returns array. I tried:

this.view_skills = function() {
        var arr = ;
        dashboard.view_skills();
        skill.approved_skills.each(function(item) {
            item.getInnerHtml().then(function(text) {
                console.log('User skills: ' + text);
                arr.push(text);
            });
            console.log('view a:' + arr);
        });
        return arr;
    };

But when I call the function, I get message that it's undefined. I tried to make var arr=[] But this returned empty array.

Thanks for the help.

Halina
  • 235
  • 1
  • 4
  • 14

1 Answers1

0

1.) You need to initialize the array, or else you get an error

2.) Promises are async, so when you start one, the JS code doesn't stop. Pretty much this hapens:

You create the arr array, then you start a promise. It takes some time so the engine goes on with the code. It returns the empty array, then a bit later the promise ends, and it puts something in the array.

Bálint
  • 4,009
  • 2
  • 16
  • 27