0

I am working with JavaScript in protractor environment for E2E tests.
I read a lot of closure and promises on JavaScript, however I still can't understand what is wrong in my code.

I try to get back from the following function specific string value from tag element (which contain few tags).

Can someone please explain what is wrong there?

In the debug messages I got the correct value, but the function always returns the initial value of group_name ( means: "tmp").

Below is the fuction I wrote in my page object:

this.retreive_participant_name_by_idx = function(idx){  
    var group_name="tmp"; 

    element.all(by.repeater(this.group_participant_repeater()))
        .then(function(rows) {          
            console.log("before assign  " + group_name);

            element.all(by.repeater("tag in tagList.items track by track(tag)"))
                .get(idx)
                .then(function(row){
                    row.getText().then(function (txt) {
                        group_name = txt;
                        console.log("Item name  " + group_name + "!!!!!");
                        return group_name;  
                    });
                }); 
            });
    return (group_name);
};
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • I guess the return gets called before promise happens. If you put console logs by steps e.g. console.log('step 1'), console.log('step 2') you will see what I mean – Ello Sep 21 '15 at 14:08
  • Welcome to SO. Please take more care in formatting your questions. You even have a snippet editor where you can tidy your JS – mplungjan Sep 21 '15 at 14:11
  • 1
    This has nothing to do with closures. This is about async function timing. – Quentin Sep 21 '15 at 14:13

0 Answers0