简体   繁体   English

Http请求完成加载后执行功能

[英]Execute a function when Http Request finishes loading

I am using XMLHttpRequests to load large amounts of data from a Flask REST API for my Machine Learning project. 我正在使用XMLHttpRequests从我的机器学习项目的Flask REST API加载大量数据。 Let's say I have the following (simplified) setup: 假设我有以下(简化)设置:

const urlData1 = 'someUrl';
const urlData2 = 'someUrl';

var data1Req = new XMLHttpRequest();
var data2Req = new XMLHttpRequest();

data1Req.addEventListener("load", data1Listener);
data2Req.addEventListener("load", data2Listener);

var data1Storage;
var data2Storage;

function data1Listener() {
    data1Storage = JSON.parse(this.responseText);
}

function data2Listener() {
    data2Storage = JSON.parse(this.responseText);
}

data1Req.open("GET", urlData1, true);
data1Req.send();

data2Req.open("GET", urlData2, true);
data2Req.send();

// How do I do something with both data1Storage and data2Storge once they are loaded?

The problem with this is that I can't know when both data has been stored to their respective variables (data1Storage and data2Storage). 问题是我不知道何时两个数据都已存储到它们各自的变量(data1Storage和data2Storage)中。 Since they are very large data, it takes quite a long time to load, and accessing them right away will return an undefined. 由于它们是非常大的数据,因此加载需要很长时间,并且立即访问它们将返回未定义的内容。 How do I manipulate these two variables only when data has been stored in them? 仅当数据已存储在两个变量中时,才如何操作它们? My first thought was to do an if statement like if (data1Storage!= undefined && data2Storage != undefined) , but that obviously does not work. 我的第一个想法是做一个if语句,如if (data1Storage!= undefined && data2Storage != undefined) ,但是显然不起作用。

My suggestion is to change your data1Listener & data2Listener functions in the following way: 我的建议是通过以下方式更改data1Listenerdata2Listener函数:

async data1Listener() {
    try {
        data1Storage = await JSON.parse(this.responseText);
        console.log('Finished putting into data1Storage');
    } catch (e) {
        throw new Error(e);
    }
}

async data2Listener() {
    try {
        data2Storage = await JSON.parse(this.responseText);
        console.log('Finished putting into data2Storage');
    } catch (e) {
        throw new Error(e);
    }
}

The async keyword in front of a function allows you to await inside of it. 函数前面的async关键字允许您在函数内部await

The await keyword will, as its name suggests, wait for the completion of the current statement before moving onto the next line. 顾名思义, await关键字将等待当前语句的完成,然后再移至下一行。

In these functions, the await appears before JSON.parse(this.responseText) . 在这些函数中, await出现在JSON.parse(this.responseText)之前。

Therefore, only once this.responseText has been fully parsed and contained by the data1Storage & data2Storage variables will the console.log lines execute. 因此,只有this.responseTextdata1Storagedata2Storage变量完全解析并包含之后, console.log行才会执行。

If an error occurs at any time inside the try block, an error will be thrown by the catch block. 如果try块内的任何时间发生错误,则catch块将引发错误。 (As an example, if this.responseText is invalid JSON and cannot be parsed, then the console.log will not run and an error instead will be printed. (例如,如果this.responseText是无效的JSON并且无法解析,则console.log将不会运行,而是显示错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM