从包含AJAX函数的JavaScript文件返回数据

从包含AJAX函数的JavaScript文件返回数据,javascript,ajax,function,dom,asynchronous,Javascript,Ajax,Function,Dom,Asynchronous,我已经创建了control.js文件,其中包含向服务器发送和获取请求的AJAX函数 问题是,我无法将从control.js文件中的AJAX函数接收到的数据返回到其他JavaScrip文件 我知道我们不能直接从异步函数返回数据。它需要额外的函数来处理从函数返回的数据 function getServer(directory) { xmlHttp.open("GET",rootDirectory+directory); xmlHttp.onreadystat

我已经创建了control.js文件,其中包含向服务器发送和获取请求的AJAX函数

问题是,我无法将从control.js文件中的AJAX函数接收到的数据返回到其他JavaScrip文件

我知道我们不能直接从异步函数返回数据。它需要额外的函数来处理从函数返回的数据

 function getServer(directory)
    {
        xmlHttp.open("GET",rootDirectory+directory);
        xmlHttp.onreadystatechange = function(){
            if(xmlHttp.readyState == 4)
            {
                if(xmlHttp.status == 200)
                {
                    Handler(xmlHttp.responseText);
                }
            }
        };
        xmlHttp.send();
    }
 function Handler(response)
 {
      return response;
 }
getServer("/foo/bar/", myFunction);
函数处理程序将数据正确返回到它自己的文件(control.js文件内)中的每个函数调用

现在,假设我有多个JavaScript文件,我想调用这个函数。如何获取此文件中函数返回的值

例如:

function construct()
{     
    // This function belongs to load.js file not control.js
    var handler = getServer("request/read.php?table=tasks");        
    console.log(handler);  // This one return nothing...//
}

处理程序
函数作为参数传递,而不是将其硬编码到函数中

 function getServer(directory)
    {
        xmlHttp.open("GET",rootDirectory+directory);
        xmlHttp.onreadystatechange = function(){
            if(xmlHttp.readyState == 4)
            {
                if(xmlHttp.status == 200)
                {
                    Handler(xmlHttp.responseText);
                }
            }
        };
        xmlHttp.send();
    }
 function Handler(response)
 {
      return response;
 }
getServer("/foo/bar/", myFunction);