来自异步的请求/响应Javascript

时间:2016-02-14 03:34:37

标签: javascript asynchronous websocket async-await rxjs

我有这段代码,我并不十分不满意,但想知道如何转换为反应范式而不是在ES7中使用async / await(因为我知道如何做到这一点,我认为)。 请原谅代码中的注释,一旦调试完成,我通常会把它们拿出来。我正在使用async.js希望它能带给我像async / await这样的东西,但不会。此外,如果有人知道javascript中类似的CountDownLatch impl,我将有兴趣了解它。

document.getElementById('submitLoginBtn').addEventListener("click", function () {
        var rqid = nextReqId();
        async.series([
            function(callback) {
                console.log("Generating password for reqid: " + rqid);
                genPartOfRequest(callback);
            },
            function(callback) {
                console.log("Adding listener for " + msg_LogonResponse + " and reqid: " + rqid);
                mHandler.addListener(msg_LogonResponse, rqid, function(msg) {
                    console.log("Listener callback called for " + msg_LogonResponse + " and reqid: " + rqid);
                    if (isOk(msg)) {
                        postLogin(msg);
                    }
                    callback(null);
                });
                console.log("Sending logon request for " + msg_LogonRequest + " and reqid: " + rqid);
                logon(msg_LogonRequest, rqid);
            },
            function(callback) {
                console.log("Removing listener for " + msg_LogonResponse + " and reqid: " + rqid);
                mHandler.removeListener(msg_LogonResponse, rqid);
                callback(null);
            }
        ]);
    });
mHandler是我创建的“类”,用于插入请求应生成的特定响应的侦听器。我正在使用websockets。

由于 d

0 个答案:

没有答案