简体   繁体   中英

How to use async.series to wait for response?

I posted this question yesterday: https://stackoverflow.com/questions/25919099/how-do-i-use-callback-to-solve-authentication-issue

Basically I want to wait for response from my login request and then go to Checkin request. Otherwise checkin request gives 401 that is authentication error.

Right now I am trying to use some library like step, wait.for or async to wait for the response. Using async.series I am trying this code but it's giving unexpected token function error at function two()

function checkin() {

    async.series[(
                function one() {
                    agent1
                        .post(login-url)
                        .type('form') // send request in form format
                        .send({
                            username: username,
                            password: password
                        })
                        .end(function(err, res) {
                            console.log("response for login is ", res.statusCode, " ", res.message);
                        });
                }

                function two() {
                    for (var i = 0; i < count; i++) {
                        if (validatePayment(rows[i].Payment) == true && validateMobile(rows[i].Mobile) == true) {
                            console.log("inside validation");
                            agent1
                                .post(checkin-url)
                                .send({
                                    phone: rows[0].Mobile,
                                    outlet: outletID
                                        //outlet: "rishi84902bc583c21000004"
                                })
                                .end(function(err, res) {
                                    console.log("response for checkins is ", res.statusCode, " ", res.message);
                                });
                        )];
                }
            }
        }
        //    });
}

You're getting an unexpected token error because you're trying to define multiple functions within a parenthesized expression. Try this line in your console:

(function one() {} function two() {})

What's going on here is you're trying to access async.series like it's an array or something:

async.series[ ...index here... ]

Then, for the index, you're passing an expression:

async.series[ (...) ];

The expression fallaciously contains two function definitions:

async.series[ ( function one() { ... } function two() { ... } ) ]

A parenthesized expression is only supposed to return one value. Two functions will compete to be that return value and are therefore invalid. But what you're doing is all wrong in the first place.

I think what you really mean is to call async.series , and pass an array of functions ...

async.series( [ function one() {...}, function two() {...} ] );

Your updated code might look like this fiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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