async.js和系列问题 [英] async.js and series issue

查看:308
本文介绍了async.js和系列问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试运行取指连接后。抓取比连接速度更快,并在控制台我得到,因为它返回的结果比连接更快地完成提取错误。但在异步系列文件是运行第二个函数返回首节省result.Settimeouts后的情况,但它不是美丽的工具。我怎么能等待,没有承诺何时全部完成?

  VAR下注= [];
async.series([
    函数(回调){
        的setTimeout(函数(){
            连接();
            回调(空,'一');
        },1)
    },
    函数(回调){
        的setTimeout(函数(){
            fetch_last_30();
            回调(NULL,两节);
        },2000)
    }
]);

UPD
我的连接功能。

 功能连接(){
    VAR URL =HTTPS://api....../login
    / *连接到网站,并获得的access_token访问其他的API * /
    $。员额(
        URL,
        {用户名:000,密码为:000},
        功能(数据){
            的access_token =数据[ACCESS_TOKEN];
            的console.log(数据[ACCESS_TOKEN]);
        }
    )
}


解决方案

您无需调用回调,直到连接()调用完成它的异步工作。这是该异步库可以完成其工作的唯一途径。由于连接()是异步的,它可能有一个回调本身,你可以用知道什么时候它实际上是完成的。

此外,你不应该需要的的setTimeout()电话如果你正确使用异步库都没有。

从概念上讲,你想这样的事情,其中​​连接()函数调用回调时通过它完成它的异步操作:

  VAR下注= [];
async.series([
    函数(回调){
        连接(函数(){
            回调(空,'一');
        });
    },
    函数(回调){
        fetch_last_30();
        回调(NULL,两节);
    }
]);

虽然,我会亲自找到许诺更好的解决方案在这里进行测序两种操作,你可以改变你的连接()函数如下:

 连接功能(回调){
    VAR URL =HTTPS://api....../login
    / *连接到网站,并获得的access_token访问其他的API * /
    $。员额(
        URL,
        {用户名:000,密码为:000},
        功能(数据){
            的access_token =数据[ACCESS_TOKEN];
            的console.log(数据[ACCESS_TOKEN]);
            回调(数据);
        }
    )
}


下面是一个使用的承诺,而不是异步库的版本:

 连接功能(回调){
    VAR URL =HTTPS://api....../login
    / *连接到网站,并获得的access_token访问其他的API * /
    返回$。员额(URL,{用户名:000,密码为:000}),然后(功能(数据){
       的access_token =数据[ACCESS_TOKEN];
       的console.log(数据[ACCESS_TOKEN]);
       返回的数据;
    });
}连接()。然后(功能(数据){
    fetch_last_30();
});

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"]);
        }
    )
}

解决方案

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();
});

这篇关于async.js和系列问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆