title: How to return result from a sequelize query thats wrapped in a function
body: <p>I am building a service that creates a unique url for a user. 
  It's supposed to do the following:</p>
  
  <blockquote>
    <ul>
    <li>query database to see if the user already has the url and send it back</li>
    <li>if user doesn't have the url create one using the user's first and last name</li>
    <li>check the database if somebody else is using that combination of string and attach a 1, 2, or 3 depending on how many other users have the name (ie. JackBlack1, JackBlack2, etc.).
    
    <blockquote>
      <p>I have one get route set up that checks for the url, if the user doesn't have one yet I want to send the result of a separate function call (for the sake of keeping the code modular). 
      My problem is with the function. It does not return the url string. I am trying to save the url string to a variable that is initiated at the top of the function but it's not being saved from the sequelize query. 
      Here is the route where I call the function:</p>
    </blockquote></li>
    </ul>
  </blockquote>
  
  <pre><code>router.get('/link', function(req, res) {
  
      db.user.find({where: {id: req.user.id} })
  
          .then(function(user){
  
          if (!user.addresslink) {
              res.send(createUserLink(req.user.id,req.user.firstName,req.user.lastName));
          } else {
  
          res.send(user.addresslink)
  
          }
      })
          .catch(function(err){
              console.log(err);
              res.status(500).send('Internal database error.');
          })
  })
  </code></pre>
  
  <p>And here is the createUserLink function:</p>
  
  <pre><code>function createUserLink(userId, first, last){
      var urlString = (first + last).toLowerCase();
      var userLink;
      var count;
  
      db.user.findAndCountAll({where: { addresslink: { $like:  'www.website.com/' + urlString } }
           }).then(function(result){
  
          if(result.count &gt; 0) {
             count = result.count + 1;
             db.user.find({where: {id: userId} }).then(function(user){
                  user.updateAttributes({
                      addresslink: 'www.website.com/' + urlString + count
                  }).then(function(result){
  
                      userLink = result.addresslink
  
                      return userLink;
                  })
  
             })
  
          } else {
              db.user.find({where: {id: userId} }).then(function(user){
                  user.updateAttributes({
                      addresslink: 'www.website.com/' + urlString
                  }).then(function(result){
  
                      userLink = result.addresslink
  
                      return userLink;
                  })
             })
          }
      })
    return userLink ;            
  }
  </code></pre>
  
  <p>I'm assuming the inability to return the created url from the function has to do with the asynchronous nature of the query. Do I need to add a query after the url has been created and then return it? This is my first question on here, so I apologize in advance if anything is inappropriate. </p>