Greg-A
Greg-A

Reputation: 862

Array with a function that returns a value

I have the following constant :

  const getActionsList = {
    listActions: [
      '$http',
      '$route',
      '$q',
      '$location',
      function($http, $route, $q, $location) {
        const id = $route.current.params.id;
        const deferred = $q.defer();
        $http.get('/test/' + id).then(
          function(result) {
            if (result.data.length > 0) {
              deferred.resolve(result.data);
            } else {
              var urlRedirect = '/';
              $location.url(urlRedirect).replace();
            }
          },
          function(result) {
            deferred.reject(result);
          }
        );
        return deferred.promise;
      }
    ]
  };

the function that is in getActionsList must return a value.

and I would like when I call it in another function that it returns a value rather than a function

var menu = function() {
    return {
      mediaType: function() {
        return getActionsList[4];//should return a value rather than a function 
      }
    };
  };

how to get getActionsList [4] to return a value ?

Upvotes: 0

Views: 59

Answers (4)

German
German

Reputation: 1166

Try my code:

var menu = function() {
  return {
    mediaType: (function() {
      return getActionsList.listActions[4](); //should return a value rather than a function
    })()
  };
};

get ActionsList is an object and in that object you put an array of listActions.

Now in your function expression menu, you want to return a mediaType and that should an anonymous function. You need to invoke the function to return value. You also need to invoke the listActions[4]. So you need to put it in this way listActions4. And since that listAction was inside of an object getActionsList that's why it will now be getActionsList.listActions4

Upvotes: 0

Sniffer
Sniffer

Reputation: 79

getActionsList[4]

will return a value of array - and it's a function. You need to invoke the function, to get result :)

getActionsList[4]()

Upvotes: 0

brk
brk

Reputation: 50291

May be like this

return getActionsList.listActions[4]()

but parameters need to be passed

Upvotes: 1

ageoff
ageoff

Reputation: 2828

Simply have mediaType use the function instead of return the function:

var menu = function() {
  return {
    mediaType: getActionsList[4];//should return a value rather than a function
  };
};

Upvotes: 0

Related Questions