[英]Nodejs and callbacks
I am very new to Node.js and how it's callbacks work exactly, I am trying to find some good documentation on it but it's just not clicking for me yet. 我是Node.js的新手,它的回调函数是如何工作的,我试图在它上面找到一些不错的文档,但现在还没有找到我。 I'm coming from python so I'll show an example of what I'm use to doing in python, I'm not sure if it's possible in node though
我来自python,所以我将显示一个示例,说明如何在python中执行操作,但不确定是否可以在node中使用
def getRequest(link):
url = urllib.request.urlopen(link).read().decode()
return url
class urlData:
def __init__(self, link):
self.results = getRequest(link)
I'm not sure if node can do this because it's async ways, or is it possible? 我不确定节点是否可以执行此操作,因为它是异步方式,还是可能? I'm not sure how to go about this the correct way, how would I replicate this action in node?
我不确定如何正确执行此操作,如何在节点中复制此操作? If not can the this code be toyed with to get similar results, a way to set the variable with the data that is going to come?
如果不能,可以用此代码来获得相似的结果,一种用即将到来的数据设置变量的方法吗?
The way you might do this in node is the following: 您可以在节点中执行以下操作:
Install Request. 安装请求。 https://github.com/mikeal/request
https://github.com/mikeal/request
var request = require('request');
Now we have a simple http client. 现在我们有了一个简单的http客户端。
var data;
request.get({url:url, json:true}, function (e, r, body) {
// this will get called when the response returns. Your app will continue before this is called.
data = body; // i have no idea what you want to do with it
console.log(body); // should be json
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.