Question: This is a pretty simple function that returns a value based on the variable `category`. I tried to use switch instead of define `menu` object that contained functions with `tag` name but I noticed that it looks pretty dirty so that's how I made this code. After that I realized defining `menu` variable will run every function in the object to store the value in the properties which is not what I'm trying to do. What I want to do is that I want to run only a function and it needs to run to get a value based on the variable `category`. How could I make this better?

```
const getContent = async (category, tag) => {
    let query = { 'tag':tag, 'limit':10 }

    let menu = {
        'feed': () => steem.api.getDiscussionsByFeed(query),
        'blog': () => steem.api.getDiscussionsByBlog(query),
        'new': () => steem.api.getDiscussionsByCreated(query),
        'hot': () => steem.api.getDiscussionsByHot(query),
        'trend': () => steem.api.getDiscussionsByTrending(query)
    }

    return await menu[category]()
}

```

Answer: 
> 
> After that I realized defining menu variable will run every function in the object to store the value in the properties which is not what I'm trying to do.
> 
> 
> 

What you have looks like it would work just fine. Declaring the `menu` structure does not execute the functions you've assigned as properties. It just defines the functions and assigns them as properties (waiting to be called later).

You could save a little execution overhead by moving the static `menu` definition outside the function so it just gets defined once (I'm assuming this is private module scope so moving the symbol outside the function is fine) and change it to `const`.

You can also remove the `async` and `await` since neither add anything here. You're just returning a promise anyway so you can just return the promise directly without `async` and `await`. I also added more explicit error checking for the category name.

```
const methodMap = {
    'feed': () => steem.api.getDiscussionsByFeed(query),
    'blog': () => steem.api.getDiscussionsByBlog(query),
    'new': () => steem.api.getDiscussionsByCreated(query),
    'hot': () => steem.api.getDiscussionsByHot(query),
    'trend': () => steem.api.getDiscussionsByTrending(query)
};

const getContent = (category, tag) => {
    const query = { 'tag':tag, 'limit':10 };
    if (methodMap.hasOwnProperty(category)) {
        return methodMap[category]();
    } else {
        return Promise.reject(new Error(`Unknown Category: ${category}`));
    }
}

```

---

You could also remove some redundancy from the table by making the table only be the steem method name you want to call.

```
const methodMap = {
    'feed': 'getDiscussionsByFeed',
    'blog': 'getDiscussionsByBlog',
    'new': 'getDiscussionsByCreated',
    'hot': 'getDiscussionsByHot',
    'trend': 'getDiscussionsByTrending'
};

const getContent = (category, tag) => {
    const query = { 'tag':tag, 'limit':10 };
    if (methodMap.hasOwnProperty(category)) {
       return steem.api[methodMap[category]](query);
    } else {
        return Promise.reject(new Error(`Unknown Category: ${category}`));
    }
}

```

This layout also does more explicit error checking on a "supported" category being passed in.
I don't know why you think switch looks dirty, I think it's perfect for this situation. You can drop the `break`, since you are returning anyway, and each case is just one function call, so you can put it all on one line. It's also easy to handle errors by just adding a single line.

```
const getContent = async (category, tag) => {
    let query = { 'tag':tag, 'limit':10 }

    switch(category) {
        case 'feed':  return steem.api.getDiscussionsByFeed(query);
        case 'blog':  return steem.api.getDiscussionsByBlog(query);
        case 'new':   return steem.api.getDiscussionsByCreated(query);
        case 'hot':   return steem.api.getDiscussionsByHot(query);
        case 'trend': return steem.api.getDiscussionsByTrending(query);
        default: return 'Error';
    }
}

```