title: How to maintain the context in javascript async call?
body: <p>I'm working with moodle version 3.2, and i want to catch all the ajax requests and delay them. I can catch them, but i can't maintain the context in which they were called. </p>
  
  <p>Example: I have 2 javascript files, <code>accessManager.js</code> and <code>filepicker.js</code>.
  The <code>accessManager.js</code> is included in all HTML files.</p>
  
  <p>My accessManager.js contains:</p>
  
  <pre><code>function myFunction(){
    return new Promise(function(resolve, reject) {
      setTimeout( function () {
        var r = confirm("Ajax Request!");
        if (r) {
          resolve(true);
        }
        else {
          reject(false);
        }
      }, 3000);
    });
  }
  </code></pre>
  
  <p>and <code>filepicker.js</code> (the important part,the ajax call) contains </p>
  
  <pre><code>myFunction().then(function(){
    Y.io(api, cfg);
   });
  </code></pre>
  
  <p>The problem lies in the ajax call that is done in a different context, and return some errors. How can i keep the ajax call context?? The expected behavior would be filepicker -> call myFunction async to delay the ajax call -> return to filepicker context and make the call.</p>
  
  <p>Thanks in advance! </p>