title: How to access outer variables from a callback function?
body: <p>I'm having trouble with the following code snippet. I can't access <code>userId</code> from within the callback function, and I can't return whether <code>whitelistedUserIds</code> contains the <code>userId</code> or not. According to the debugger, when I step inside the callback, <code>userId</code> is undefined.</p>
  
  <p>Could anyone explain why? and how to fix this? I've been off javascript for quite a while...</p>
  
  <pre><code>function userInWhitelist(userFileName) {
    var userId = userFileName.replace('.txt', '');
    request({
      url: whitelistURL
    }, function(err, resp, body, userId) {
      if (resp.statusCode == 200) {
        var users = JSON.parse(body).data;
        var whitelistedUserIds = _.map(users, (user) =&gt; { return user.id; });
        // How to access userId ??
        // How to return whitelistedUserIds.includes(userId)
      }
    });
  </code></pre>