I'm new to JavaScript, I need to refactor some code that looks like this:
    [initial code - resolves condition]
    if(condition) {
      $.get("[api1]", function(result) {
          doStuffWith(result);
       });
    }
    else {
      $.get("[api2]", function(result) {
          doStuffWith(result); // <-- same stuff as before
       });
    }
I want to refactor it to something like the following:
 function getResult() {
       [initial code - resolves condition]
       if(condition) {
          $.get("[api1]", function(result) {
              res = result;
          });
        }
        else {
          $.get("[api2]", function(result) {
            res = result;
          });
       } 
       return res;
     }
...
var result = getResult();
doStuffWith(result);
I need this partly for readability but mostly because I need to reause the getResult() functionality in another place.
Problem is, the ajax calls - $.get() are asynchronous. The function they take will execute at some point in the future when the ajax call returns.
How do I turn this into a reusable bit of code? How do I then use it?
