I'm working with an API wrapper whose endpoints only accept user ID's when querying - however, there's a single endpoint that lets me search by username (the wrapper for which, searchByUsername, you'll see used below) I wrote the following helper function to convert them before every request fires, to give our users flexibility:
  var convertToID = function (username, callback) {
    searchByUsername(username, function (error, response, body) {
      if (body.users[0]){
        callback(body.users[0].username)
      }
    })
  };
And here's how I'm using it:
  this.relationship = function (user, query, callback) {
    if (isNaN(user)) {
      convertToID(user, function (ID) {
        get('/users/' + ID + '/relationships/?query=' + query, callback);
      })
    } else {
      get('/users/' + user + '/relationships/?query=' + query, callback);
    }
  }
This works fine, but it's obviously sub-optimal to drop this into every one of my wrappers. Coming from a Ruby background, I could condense this to a single line - but the async nature of JS is kind of twisting my brain. What I really want is something comparable to:
var user = isNaN(user) ? convertUsernameToID(user) : user;
get('/users/' + user + '/relationships/?query='+query, callback);
I've explored the other answers (and yes, I know there's a lot of them) re: async JS gotchas, but I must be missing something. In the past on larger projects, I've used the async package but it feels like overkill for this and I'd prefer to actually grok this.
 
     
     
    