Javascript 我可以吗;“排队”;XMLHttpRequest函数?

Javascript 我可以吗;“排队”;XMLHttpRequest函数?,javascript,xmlhttprequest,Javascript,Xmlhttprequest,我需要获得可用文件(存储在目录中)和已处理文件(存储在数据库表中)的列表 我有一些JavaScript调用Python脚本并将其(JSON)输出解析为对象。无论Python处理的是目录文件还是数据库表,这都非常有效 function runGetList() { // request external Python script (output in JSON) var handleResponse = function(status, response) { /

我需要获得可用文件(存储在目录中)和已处理文件(存储在数据库表中)的列表

我有一些JavaScript调用Python脚本并将其(JSON)输出解析为对象。无论Python处理的是目录文件还是数据库表,这都非常有效

function runGetList() {
    // request external Python script (output in JSON)
    var handleResponse = function(status, response) {
        // save JSON-from-Python as an object
        var jsonMDBList = xhr.response;
        // do stuff with the object data (build select box, build table)
    }
    var handleStateChange = function() {
        switch (xhr.readyState) {
            case 0: break; // uninitialized
            case 1: break; // loading
            case 2: break; // loaded
            case 3: break; // interactive
            case 4: // completed
                handleResponse(xhr.status, xhr.response);
                break;
            default: alert("unspecified error");
        }
    }
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = handleStateChange;
    xhr.open("GET", "python/GetListMDBs.py", true);
    xhr.responseType = "json";
    xhr.send();
}
但是,现在我已经到了一个点,我将拥有多个对象(例如,一个来自
GetListMDBs.py
,一个来自
GetTable.py
),并在最终HTML的不同部分列出它们。我最初的想法是将
runGetList()
变成一个通用函数,可以使用我需要的特定Python脚本的参数调用该函数

function getAllLists() {
    var mdbListAll = runGetList('GetListMDBs.py');
    console.log("list mdbs: " + mdbListAll);
    var mdbListTbl = runGetList('GetTable.py');
    console.log("table mdbs: " + mdbListAll);
}
function runGetList(filename) {
    // etc.
}
这是正确执行并从各个Python脚本获取对象。但是
getAllLists()
函数并不等待
返回
ed对象,它只是继续运行并记录
list MDB:undefined


在我的方法中,我能做些什么改变,让脚本在移动到下一个HTTP请求之前等待每个HTTP请求完成(并处理输出)?我想到的唯一解决方法是一个更大、更复杂的JSON对象,它包含我想要嵌入的所有列表,但我不希望这样做,因为这些Python脚本被一些应用程序使用,我正试图保持它的流线型,而不是到处复制代码。

尝试使用
Promise

   function runGetList() {
     var p = new Promise(function(resolve, reject) {
       // request external Python script (output in JSON)
       var handleResponse = function(status, response) {
         // save JSON-from-Python as an object
         var jsonMDBList = xhr.response;
         // do stuff with the object data (build select box, build table)
         resolve(jsonMDBList)
       }
       var handleStateChange = function() {
         switch (xhr.readyState) {
           case 0:
             break; // uninitialized
           case 1:
             break; // loading
           case 2:
             break; // loaded
           case 3:
             break; // interactive
           case 4: // completed
             handleResponse(xhr.status, xhr.response);
             break;
           default:
             alert("unspecified error");
         }
       }
       var xhr = new XMLHttpRequest();
       xhr.onreadystatechange = handleStateChange;
       xhr.open("GET", "python/GetListMDBs.py", true);
       xhr.responseType = "json";
       xhr.send();
     });
     return p
   }

   function getAllLists() {
     var mdbListAll = runGetList('GetListMDBs.py');
     var mdbListTbl;
     console.log("list mdbs: " + mdbListAll);
     mdbListAll.then(function(data) {
       console.log("table mdbs: " + data);
       mdbListTbl = runGetList('GetTable.py');
       return mdbListTbl
     })
   }
基于异步请求,但具有更好的异步请求链接:

function runGetList(filename) {
    var p = new Promise(function(resolve, reject) {
        // do stuff to get jsonMDBList
        resolve(jsonMDBList)
    }};
    return p;
}

function getAllLists() {
    var mdbListAll = runGetList('GetListMDBs.py'); // returns promise 1
    mdbListAll.then(function(data) {               // runs when promise 1 resolves
        console.log("list mdbs: " + data);
        mdbListTbl = runGetList('GetTable.py');    // returns promise 2
        return mdbListTbl
    }).then(function(data) {                       // runs when promise 2 resolves
        console.log("table mdbs: " + data);
    });
}

Promise
有效,但我需要调整调用。我试图编辑你的答案,但因“偏离原意”而被拒绝:P