0

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 ?

asked Mar 6, 2020 at 16:32

4 Answers 4

1

May be like this

return getActionsList.listActions[4]()

but parameters need to be passed

answered Mar 6, 2020 at 16:37
Sign up to request clarification or add additional context in comments.

Comments

0

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
 };
};
answered Mar 6, 2020 at 16:34

Comments

0
getActionsList[4]

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

getActionsList[4]()
answered Mar 6, 2020 at 16:39

Comments

0

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

answered Mar 6, 2020 at 16:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.