async.js函数的排序

时间:2017-12-12 03:31:03

标签: javascript node.js async.js

所以我在下一个启动之前无法获得一个 javascript 功能。我花了很多时间尝试使用其他stackoverflow帖子中描述的回调方法。我可以获得使用超时工作的简单示例,但无法使用我的API请求。我偶然发现async.js,并认为使用async.series可能是一个好主意,让我的两个功能一个接一个地执行。所以我尝试了这种方法,但是我似乎仍然遇到第一个函数需要更长时间才能执行的问题(这很好),但是执行过程移过这个函数而不是等待它结束。我觉得我有某种误解,因为我尝试了几种方法,但无济于事。

奇怪的是,在运行server.js时,它会进入第一个函数,但在请求完成之前它会离开async.series()函数。当我在tokenReq()内打印时,我可以看到请求成功,因为令牌代码被成功返回,但是这种情况发生在执行已经开始的时候。输出如下所示。

server.js:

var access_code;
async.series([
    function() {
        access_code = queries.data.tokenReq(code);
        console.log("Finished inside function 1");
    },
    function() {
        console.log("\n Starting function 2 \n");

        if (access_code === "error") {
            res.json("An error has occured");
        } else {
            var response = queries.data.messagesReq(access_code);
            res.json(response);
        }
    }
],
function(err, access_code) {
});

console.log("Outside");

queries.js:

tokenReq: function(code) {
    var tokenUrl = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
    var form = {
        code: code,
        client_id: "__ID__",
        redirect_uri: "__Site__/",
        grant_type: "authorization_code",
        client_secret: "__Secret__",
    };


    var formData = querystring.stringify(form);
    var contentLength = formData.length;

    request({
            headers: {
            'Content-Length': contentLength,
            'Content-Type': 'application/x-www-form-urlencoded'
            },
            uri: tokenUrl,
            body: formData,
            method: 'POST'
        }, function (error, response, body) {

        if (error != "null") {
            var access_token = JSON.parse(body).access_token;
            console.log("\n INSIDE FUNCTION REQUEST, Token: " + access_token + " \n");
            return access_token;

        } else {
            console.log('error:', error); // Print the error if one occurred
            console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
            //   console.log('body:', body); // Print the HTML for the Google homepage.
            return "error";
        }
    });
},

输出:

Finished inside function 1
Outside

INSIDE FUNCTION REQUEST, Token: 8Swhd.......

1 个答案:

答案 0 :(得分:0)

你错过了一个重点。由于node.js是异步的,因此不应该知道函数何时完成其执行。这就是为什么我们指定回调,以便调用函数知道在完成执行时要调用谁。使用回调函数后,可以使用async模块强制执行系列/并行/瀑布行为。

tokenReq: function(code, cb) {
    var tokenUrl = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
    var form = {
        code: code,
        client_id: "__ID__",
        redirect_uri: "__Site__/",
        grant_type: "authorization_code",
        client_secret: "__Secret__",
    };


    var formData = querystring.stringify(form);
    var contentLength = formData.length;

    request({
        headers: {
            'Content-Length': contentLength,
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        uri: tokenUrl,
        body: formData,
        method: 'POST'
    }, function (error, response, body) {

        if (error != "null") {
            var access_token = JSON.parse(body).access_token;
            console.log("\n INSIDE FUNCTION REQUEST, Token: " + access_token + " \n");
            return cb(null, access_token);

        } else {
            console.log('error:', error); // Print the error if one occurred
            console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
            //   console.log('body:', body); // Print the HTML for the Google homepage.
            return cb(new Error("whatever"));
        }
    });
},

现在,您可以在server.js

中使用回调
var access_code;
async.series([
    function(cb) {
        return queries.data.tokenReq(code, cb);
    },
    function(access_code, cb) {
        console.log("\n Starting function 2 \n");

        if (access_code === "error") {
            res.json("An error has occured");
        } else {
            var response = queries.data.messagesReq(access_code);
            res.json(response);
        }
        // do whatever you want after this
        return cb();
    }
],
function(err, access_code) {
    if (err) {
        console.log(err);
    }
    // wrap your logic around a function and call the correspoding callback here
});