title: Export firing before the function return, how can I fix this?
body: <p>I have this function get the token from api and check if I have admin permission. The problem is my export its firing before of the checking of the function.</p>
  <pre><code>const async = [
    { path: '*', redirect: '/404', hidden: true }
  ]
       
      var hasAdmin = getAdmin()
          if (hasAdmin === undefined || hasAdmin === null) {
            var token = null
            var url = null
            var uri = window.location.href.split('?')
            if (uri.length === 2) {
              var vars = uri[1].split('&amp;')
              var getVars = {}
              var tmp = ''
              vars.forEach(function(v) {
                tmp = v.split('=')
                if (tmp.length === 2) {
                  getVars[tmp[0]] = tmp[1]
                }
                token = getVars.AUTH_ID
                url = getVars.DOMAIN
              })
              getUserAdmin(url, token)
                .then(response =&gt; {
                  var hasAdmin = response.result
                  if (hasAdmin === true) {
                  console.log('hasAdmin: ' + hasAdmin)
                    async.push(adminRouter)
                  }
                })
                .catch(error =&gt; {
                  console.log(error)
                })
            }
          } else if (hasAdmin === true) {
            async.push(adminRouter)
          }
      
      export const asyncRoutes = async
      console.log('hasAdmin2: ' + hasAdmin)
  </code></pre>
  <p>Here is how Im using the const asyncRoutes:</p>
  <pre><code>import { asyncRoutes, constantRoutes } from '@/router'
  
  /**
   * Use meta.role to determine if the current user has permission
   * @param roles
   * @param route
   */
  function hasPermission(roles, route) {
    if (route.meta &amp;&amp; route.meta.roles) {
      return roles.some(role =&gt; route.meta.roles.includes(role))
    } else {
      return true
    }
  }
  
  /**
   * Filter asynchronous routing tables by recursion
   * @param routes asyncRoutes
   * @param roles
   */
  export function filterAsyncRoutes(routes, roles) {
    const res = []
  
    routes.forEach(route =&gt; {
      const tmp = { ...route }
      if (hasPermission(roles, tmp)) {
        if (tmp.children) {
          tmp.children = filterAsyncRoutes(tmp.children, roles)
        }
        res.push(tmp)
      }
    })
  
    return res
  }
  
  const state = {
    routes: [],
    addRoutes: []
  }
  
  const mutations = {
    SET_ROUTES: (state, routes) =&gt; {
      state.addRoutes = routes
      state.routes = constantRoutes.concat(routes)
    }
  }
  const actions = {
    generateRoutes({ commit }, roles) {
      return new Promise(resolve =&gt; {
        let accessedRoutes
        if (roles.includes('admin')) {
          accessedRoutes = asyncRoutes || []
        } else {
          accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
        }
        commit('SET_ROUTES', accessedRoutes)
        resolve(accessedRoutes)
      })
    }
  }
  
  export default {
    namespaced: true,
    state,
    mutations,
    actions
  }
  </code></pre>
  <p>I always need reload(f5) the page in first access to gets the menu admin.</p>
  <p>Await from promise dont works because de export should be always in the top. Some idea how can I fix this?</p>
  <p>obs: attention in variable hasAdmin</p>
  <p><a href="https://i.stack.imgur.com/89I1t.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/89I1t.png" alt="enter image description here" /></a></p>