How to return a list of available options in a select element in Protractor?

I am trying to retrieve all the options available in select element using Protractor. I am having trouble figuring out how to do this because I’m a JavaScript beginner and therefore am having trouble determining how to return the array and not a promise.

I’ve tried this:

getOptions = function () {
    var self = this;
    var availableOptions = [];

    return this.selector.click().then(function () {
        self.selectorOptions.then(function (options) {
            options.forEach(function (option) {
                option.getAttribute("value").then(function (value) {
                    availableOptions.push(value);
                });
            });
        });
    }).then(function () {
        return availableOptions;
    });
};

This returns a promise and not the array which I want. I also tried this:

getOptions = function () {
    var self = this;
    var availableOptions = [];

    this.selector.click().then(function () {
        self.cloudletTypeOptions.then(function (options) {
            options.forEach(function (option) {
                option.getAttribute("value").then(function (value) {
                  availableOptions.push(value);
                });
            });
        });
    });

    return availableOptions;
};

When I do this and call the method from a test and try to print the result, it is ‘undefined’. What do I need to do to ensure that the return value is defined and is an array? Barring that, how do I force the promise to resolve?


Source: stackoverflow-javascript