Javascript nodej和回调
我对Node.js和它的回调是如何工作的非常陌生,我试图找到一些关于它的好文档,但它还没有为我点击。我来自python,所以我将展示一个我在python中使用的示例,但我不确定在node中是否可行Javascript nodej和回调,javascript,node.js,python-3.x,Javascript,Node.js,Python 3.x,我对Node.js和它的回调是如何工作的非常陌生,我试图找到一些关于它的好文档,但它还没有为我点击。我来自python,所以我将展示一个我在python中使用的示例,但我不确定在node中是否可行 def getRequest(link): url = urllib.request.urlopen(link).read().decode() return url class urlData: def __init__(self, link): sel
def getRequest(link):
url = urllib.request.urlopen(link).read().decode()
return url
class urlData:
def __init__(self, link):
self.results = getRequest(link)
我不确定node是否能够做到这一点,因为它是异步的,还是可能的?我不确定如何以正确的方式执行此操作,如何在节点中复制此操作?如果不是的话,这个代码可以被玩弄以获得类似的结果,一种用即将到来的数据设置变量的方法 在节点中执行此操作的方法如下: 安装请求 现在我们有了一个简单的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
})
嗯,我并不想打印正文或接收到的信息,我只想将数据设置为一个变量供以后使用,但感谢您的回答。我将查看该请求模块的文档,看看是否有任何内容解释如何使用信息设置变量。控制台仅用于演示目的。你的应用程序将使用它做一些事情。什么,我知道这就是为什么在答案中放一个console.log是很常见的。
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
})