处理多个呼叫异步回调 [英] Handling multiple call asynchronous callbacks

查看:114
本文介绍了处理多个呼叫异步回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习与 learnyounode Node.js的。
我有问题ASYNC杂耍
问题被描述如下:

您将得到三个URL作为命令行参数。你应该做 http.get()调用获得来自这些网址的数据,然后以相同的顺序它们在参数列表顺序打印出来。
这里是我的code:

I am learning node.js with learnyounode. I am having a problem with JUGGLING ASYNC. The problem is described as follows:
You are given three urls as command line arguments. You are supposed to make http.get() calls to get data from these urls and then print them in the same order as their order in the list of arguments. Here is my code:

var http = require('http')
var truecount = 0;
var printlist = []
for(var i = 2; i < process.argv.length; i++) {
    http.get(process.argv[i], function(response) {
    var printdata = "";
    response.setEncoding('utf8');
    response.on('data', function(data) {
        printdata += data;
    })
    response.on('end', function() {
        truecount += 1
        printlist.push(printdata)
            if(truecount == 3) {
            printlist.forEach(function(item) {
                console.log(item)
            })
            }
    })
    })
}

下面是我不理解的问题:
我试图存储在(){}'端',函数()使用字典的每个URL完成的数据response.on。但是,我不知道如何让网址为 http.get()。如果我可以做一个局部变量中 http.get(),这将是伟大的,但我觉得每当我声明一个变量 VAR URL ,它将始终指向最后浏览的网址。因为它是全球,它通过环保持更新。什么是对我来说,这些已完成数据的数据存储与关键等于网址是什么?

Here is the questions I do not understand: I am trying to store the completed data in response.on('end', function(){})for each url using a dictionary. However, I do not know how to get the url for that http.get(). If I can do a local variable inside http.get(), that would be great but I think whenever I declare a variable as var url, it will always point to the last url. Since it is global and it keeps updating through the loop. What is the best way for me to store those completed data as the value with the key equal to the url?

推荐答案

我已经把这个 GIST 这是我怎么会去解决这个问题。它添加了请求对每个请求的开始的数组,这样做一次,我可以通过响应知道他们是按照正确的顺序迭代很好

I've put together this GIST which is how I would go about solving the problem. It adds the requests to an array on the start of each request, that way once done I can iterate nicely through the responses knowing that they are in the correct order.

在与异步处理处理,我觉得更容易去思考每一个过程看成是一个具体的开始和结束。如果您需要请求的顺序是preserved那么条目必须在创建每个进程的进行,然后再回过头来参考一下该记录上完成。只有这样,你能保证你有事情以正确的顺序。

When dealing with asynchronous processing, I find it much easier to think about each process as something with a concrete beginning and end. If you require the order of the requests to be preserved then the entry must be made on creation of each process, and then you refer back to that record on completion. Only then can you guarantee that you have things in the right order.

如果你是不顾一切地使用上面的方法,那么你可以定义你的GET回调闭包内的变量,并用它来储存的网址,这样你就不会与上次URL重写你的变量结束。如果你这样做,虽然走这条路,你会大大增加你的开销,当你不得不用你的网址,process.argv访问的顺序每个响应。我不会建议吧。

If you were desperate to use your above method, then you could define a variable inside your get callback closure and use that to store the urls, that way you wouldn't end up with the last url overwriting your variables. If you do go this way though, you'll dramatically increase your overhead when you have to use your urls from process.argv to access each response in that order. I wouldn't advise it.

这篇关于处理多个呼叫异步回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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