title: How to change javascript variable at appropriate scope based on success/failure of ajax get request
body: <p>I want to edit a javascript variable based on whether or not a file exists. Can I achieve something like this?</p>
  
  <pre><code>     var url="Content/Features/column1.html";
       var url2="";
       $.ajax({
              url: url,
              error: function()
              {   
                 url2 = "something";
              },
              success: function()
              {
                  url2 = "something else";
              }
          }); 
  </code></pre>
  
  <p>My understanding is that the typical issue here is the async in ajax. Should I be using a callback method or a promise in some fashion?</p>
  
  <p>Maybe something like:</p>
  
  <pre><code>var url="Content/Features/column1.html";
       var url2="";
  var result = $.ajax({
          url: url,
          error: function()
          {   
             url2 = "something";
             handleData(url2);
          },
          success: function()
          {
              url2 = "something else";
              handleData(url2);
          }
      }); 
  
  response = function handleData( responseData ) {
      return responseData;
  }   
  </code></pre>
  
  <p>Or even:</p>
  
  <pre><code>var url="Content/Features/column1.html";
  var url2="";
  function test(url) {
      return $.ajax(
      {
          url: url
      }
      );
  }
  
  test().done(function(url2) {
      url2= "something"
  
  }).fail(function() {
      url2= "something else"
  }); 
  </code></pre>
  
  <p>How can I change url2 at the same scope in which it was declared? Or is there a way to return a value from a callback function and change url2 based on that?</p>
  
  <p>Alternatively, is there a better way to check if this file exists? (And before you tell me that browsers don't allow ajax requests to local files, please know that I am using node-webkit, not a browser)</p>
  
  <p>None of these attempts worked quite right, but please let me know if I'm close. Thank you so much for your time.                                                </p>