1

我正在为 Gnome DE 开发简单的扩展,并且在处理异步 Soup 调用和事件循环时遇到了一些麻烦。

这是我所拥有的:

_httpSession = new Soup.Session();

let token = 'sometoken'
let url = 'someurl';
let _allData = [];
let elements = [1,2];

for (let el of elements) {
    let message = Soup.form_request_new_from_hash('GET', url + el, { access_token: token });
    _httpSession.queue_message(message, () => {
        if (message.status_code != Soup.KnownStatusCode.OK) {
            _error(message.status_code.toString());
        }
        try {
            message = JSON.parse(message.response_body.data).items;
        } catch (e) {
            _error(e.toString());
        }
        _allData = _allData.concat([el, message]);
    });
}

鉴于上述 for 循环中的异步调用,如何确保_allData.concat()已针对所有迭代执行?我想打印出_allData变量,但仅在执行每个el的连接时。

4

1 回答 1

2

最简单的方法可能是异步等待模式:

// Your request function can be turned into a Promise:
function requestFunc(session, message) {
    return new Promise((resolve, reject) => {
        session.queue_message(message, () => {
            try {
                if (message.status_code === Soup.KnownStatusCode.OK) {
                    let result = JSON.parse(message.response_body.data);

                    resolve(result);
                } else {
                    reject(new Error(message.status_code.toString()));
                }
            } catch (e) {
                reject(e);
            }
        });
    });
}

// Then you can await each in a loop
async function logRequestResults(session, url, token, elements) {
    try {
        let results = [];

        for (let el of elements) {
            let message = Soup.form_request_new_from_hash('GET', url + el, {
                access_token: token
            });

            let result = await requestFunc(session, message);

            results = results.concat([el, results.items]);
        }

        // Success; all requests completed
        log(results);
    } catch (e) {
        // An error occurred somewhere in the loop
        logError(e);
    }
}

// Using the function
_httpSession = new Soup.Session();

let token = 'sometoken'
let url = 'someurl';
let elements = [1,2];

logRequestResults(session, url, token, elements);

根据您对结果的实际处理,您可能需要对其进行重构。重要的部分是在循环中将func(something, () => {})模式变成 Promise你可以的。await

另请注意,async函数隐式返回 a Promise,那些也可以使用await

于 2020-02-13T21:14:44.427 回答