Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js承诺使用Q_Javascript_Node.js_Promise_Q - Fatal编程技术网

Javascript Node.js承诺使用Q

Javascript Node.js承诺使用Q,javascript,node.js,promise,q,Javascript,Node.js,Promise,Q,我有一个我正在编写的节点应用程序,我需要使用异步调用的承诺 我目前有一个foreach循环,它是从promise的.then(function())中调用的,但是当我返回foreach的最终结果时,我什么也得不到 在foreach中,我可以console.log数据的值并检索它,但在返回之前不能在for循环之外 var Feeds = function(){ this.reddit = new Reddit(); } Feeds.prototype.parseRedditData =

我有一个我正在编写的节点应用程序,我需要使用异步调用的承诺

我目前有一个foreach循环,它是从promise的.then(function())中调用的,但是当我返回foreach的最终结果时,我什么也得不到

在foreach中,我可以console.log数据的值并检索它,但在返回之前不能在for循环之外

var Feeds = function(){
    this.reddit = new Reddit();
}

Feeds.prototype.parseRedditData = function(){
    var _this      = this;

    this.getData(this.reddit.endpoint).then(function(data){
        return _this.reddit.parseData(data, q);
    });
}

Feeds.prototype.getData = function(endpoint){
    var deferred = q.defer();

    https.get(endpoint, function(res) {
        var body = '';

        res.on('data', function(chunk) {
            body += chunk;
        });

        res.on('end', function() {
           deferred.resolve(JSON.parse(body));
        });
    }).on('error', function(e) {
        deferred.reject(e);
    });

    return deferred.promise;
}

var Reddit = function(){
    this.endpoint = "https://www.reddit.com/r/programming/hot.json?limit=10";
}

Reddit.prototype.parseData = function(json, q){
    var dataLength  = json.data.children.length,
        data        = [];

    for(var i = 0; i <= dataLength; i++){
        var post    = {};

        post.url    = json.data.children[i].data.url;
        post.title  = json.data.children[i].data.title;
        post.score  = json.data.children[i].data.score;

        data.push(post);
    }


    return data;
}
var Feeds=function(){
this.reddit=新的reddit();
}
Feeds.prototype.parseRedditData=函数(){
var_this=这个;
this.getData(this.reddit.endpoint).then(函数(数据){
返回_this.reddit.parseData(数据,q);
});
}
Feeds.prototype.getData=函数(端点){
var deferred=q.deferred();
https.get(端点、函数(res){
变量体=“”;
res.on('data',函数(块){
body+=块;
});
res.on('end',function(){
deferred.resolve(JSON.parse(body));
});
}).on('error',函数(e){
延期。拒绝(e);
});
回报。承诺;
}
var Reddit=函数(){
this.endpoint=”https://www.reddit.com/r/programming/hot.json?limit=10";
}
Reddit.prototype.parseData=函数(json,q){
var dataLength=json.data.children.length,
数据=[];
对于(var i=0;i
当我看到这一点时,我在承诺的回调中看到了“回报”…我不知道你为什么要这样做,但我只是想确定:

我想让这个“return”成为函数“parseRedditData”的返回值,但这不起作用

此处返回数据的唯一方法是使用回调或承诺,如下所示:

Feeds.prototype.parseRedditData = function(callack){
    var _this      = this;

    this.getData(this.reddit.endpoint).then(function(data){
        callback(_this.reddit.parseData(data, q));
    });
}

我看不到Q的
用法。那么
函数或
forEach
在代码中的任何地方?Q:调用Node.js时是否使用“-harmony”标志?Q:是否有任何错误处理程序(或任何类型的错误检查)?此处是
this.getData
?@BenjaminGruenbaum更新为
this.getData
。未传入任何“--harmony”标志。@jacobalk在
parseRedditData
中的
此.getData
之前缺少一个
返回值。
Feeds.prototype.parseRedditData = function(callack){
    var _this      = this;

    this.getData(this.reddit.endpoint).then(function(data){
        callback(_this.reddit.parseData(data, q));
    });
}