在循环中使用“q”

时间:2014-11-20 16:38:52

标签: javascript node.js callback promise q

我试图从Mssql服务器中获取一些数据。我有一个带循环的函数。此函数将一些数据返回给回调。我怎样才能把数据来自kriskowal" q"到结果集变量?

函数 pet.getNameByID adress.getNameByID 将数据返回给回调函数!

感谢您的帮助!

问候Nils

exports.get = function(callback) {
edge.func("sql", {
    source: 'SELECT * FROM people'
})(null, function(error, result) {
    if (error) {
        throw (error)
    }

    if (result) {

        var resultset = []

        for (var i in result) {

            var row = result[i];

            q.all([
                // get pet name
                q.fcall(function() {
                    var deferred = q.defer();
                    pet.getNameByID({
                        id: 32155
                    }, function(data) {
                        deferred.resolve(data);
                    });
                    return deferred.promise;
                }),
                // get adress
                q.fcall(function() {
                    var deferred = q.defer();
                    adress.getNameByID({
                        id: 23
                    }, function(data) {
                        deferred.resolve(data);
                    });
                    return deferred.promise;
                }),
            ]).spread(function(resultPet, resultAdress) {

                // Data!!!!
                return {
                    petData: resultPet,
                    adressData: resultAdress
                };

            });

            resultset.push( /* How can i push the data return from spread() into the resultset array? */ )
        }

        callback(resultset);
    }
});
});

1 个答案:

答案 0 :(得分:0)

您在某些方面的结构仍然看起来像是同步方法。我试着在下面改变它:

exports.get = function(callback) {
edge.func("sql", {
    source: 'SELECT * FROM people'
})(null, function(error, result) {
    if (error) {
        throw error;
        return;
    }

    if (result) {

        var subresults = [];

        for (var i=0; i<result.length; i++) {

            var row = result[i];

            // defers for the results of the subqueries
            var petResult = q.defer(),
                adressResult = q.defer();

            // 1st subquery
            pet.getNameByID({
                id: 32155
            }, function(data) {
              petResult.resolve(data);
            });

            // 2nd subquery
            adress.getNameByID({
                id: 23
            }, function(data) {
                adressResult.resolve(data);
            });

            // collect both subquerys and add a promise for
            var subqueries = q.all( petResult.promise, adressResult.promise )
                                .then( function( pet, adress ){
                                  return {
                                    petData: pet,
                                    adressData: adress
                                  }
                                }, function( e ){ 
                                  return e 
                                });

            // collect all subresults
            subresults.push( subqueries );
        }

        // when all queries are resolved
        q.all( subresults )
         .then( function(){
          // all results will be in the arguments array here
          callback( arguments );
         }, function( e ) {
           // some error handling
         });

    }
});
});

adress.getNameByID()pet.getNameByID()的调用也是异步的,应该这样对待。所以在我的代码中,我得到了这些子查询的结果,并将它们组合成一个新的延迟/保证。当所有这些子查询承诺都被填满时,可以使用相应的结果执行主回调。