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

时间:2016-01-11 15:56:14

标签: javascript xmlhttprequest

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

我有一些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()函数不等待那个return ed对象,它只是继续运行并记录list mdbs: undefined

我可以在我的方法中更改什么,因此脚本会等待每个HTTP请求完成,然后再转到下一个(并使用输出执行操作)?我想到的唯一解决方法是一个更大,更复杂的JSON对象,所有我希望嵌入的列表,但我宁愿不这样做,因为那些Python脚本被一些应用程序使用,我试图保留它精简而不是复制代码。

2 个答案:

答案 0 :(得分:1)

尝试使用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
     })
   }

答案 1 :(得分:1)

基于the answer from guest271314,但更好地链接异步请求:

from bidi.algorithm:
import get_display
print get_display(text)