0

Try to run fetch after connect. Fetch is faster than connect, and in console I am getting fetch error because it returns result faster than connection done. But in documentation of async series is a tool to run second function after first returns result.Settimeouts saves situation, but its not beautifull. How can I wait, when all done without promises?

var bets = [];
async.series([
    function(callback){
        setTimeout(function(){
            connect();
            callback(null, 'one');
        },1)
    },
    function(callback){
        setTimeout(function(){
            fetch_last_30();
            callback(null, 'two');
        },2000)
    }
]);

UPD my connect function

function connect(){
    var url = "https://api....../login";
    /* connect to site and get access_token to access other api*/
    $.post(
        url,
        {username: "000", password : "000"},
        function(data){
            access_token = data["access_token"];
            console.log(data["access_token"]);
        }
    )
}
user3003873
  • 543
  • 1
  • 4
  • 21

1 Answers1

1

You will need to not call the callback until the connect() call completes its asynchronous work. That's the only way that the async library can do its job. Since connect() is asynchronous, it likely has a callback itself that you can use to know when it is actually done.

Furthermore, you shouldn't need the setTimeout() calls at all if you use the async library properly.

Conceptually, you would like something like this where the connect() function calls a passed in callback when it finishes its async operation:

var bets = [];
async.series([
    function(callback){
        connect(function() {
            callback(null, 'one');
        });
    },
    function(callback){
        fetch_last_30();
        callback(null, 'two');
    }
]);

While, I'd would personally find promises a better solution here for sequencing two operations, you could change your connect() function to this:

function connect(callback){
    var url = "https://api....../login";
    /* connect to site and get access_token to access other api*/
    $.post(
        url,
        {username: "000", password : "000"},
        function(data){
            access_token = data["access_token"];
            console.log(data["access_token"]);
            callback(data);
        }
    )
}

Here's a version using promises instead of the async library:

function connect(callback){
    var url = "https://api....../login";
    /* connect to site and get access_token to access other api*/
    return $.post(url, {username: "000", password : "000"}).then(function(data){
       access_token = data["access_token"];
       console.log(data["access_token"]);
       return data;
    });
}

connect().then(function(data) {
    fetch_last_30();
});
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Yes, I can detect connection callback, but can't understand how to do it in async. Cycle while-if may be? – user3003873 Apr 05 '15 at 06:51
  • @user3003873 - can you give the `connect()` function a callback that is called when it completes? You can't while/if in Javascript to wait for an async operation to complete. Due to Javascript's single threaded nature, that will not work. Please expose the code behind the `connect()` function if you want more help. – jfriend00 Apr 05 '15 at 06:53
  • Thanks! My js understanding became better! – user3003873 Apr 05 '15 at 07:12