sails.js通过async.js查找查询,每个查询都是并行调用,每个调用都提前返回



帆v0.11.0(http://sailsjs.org/)

我尝试过使用.exec回调,但没有成功,promise(http://sailsjs.org/documentation/reference/waterline-orm/queries)现在是async.js(https://github.com/caolan/async)以控制围绕查找查询循环的异步流。async.each日志输出中没有并行工作(尽管并行确实填充(。

所以,如果你的解决方案使用.exec回调、promise或async.js,我会很乐意接受的!

我发现这个链接提供了一些有用的async.js示例(http://www.sebastianseilund.com/nodejs-async-in-practice)

感谢您的时间和帮助。

下面是我使用异步的代码:

/**
 * @module      :: Service
 * @type {{findProfile: Function}}
 */
require('async');
module.exports = {
  getProfile: function (userId, callback) {
    var json = {};
    json.notFound = false;
    json.locations = {};
    json.sports = {};
    User.findOne({id: userId}).exec(function (err, user) {
      if (err) {
        json.notFound = true;
        json.err = err;
      }
      if (!err) {
        json.user = user;
        UserSport.find({user_id: user.id}).exec(function (err, userSports) {
          if (err) {
            sails.log.info("userSports error: " + userSports);
          }
          async.each(userSports, function (userSport, callback) {
            LocationSport.findOne({id:userSport.locationsport_id}).exec(function (err, locationSport) {
              if (locationSport instanceof Error) {
                sails.log.info(locationSport);
              }
              async.parallel(
                [
                  function (callback) {
                    Location.findOne({id:locationSport.location_id}).exec(function (err, location) {
                      if (location instanceof Error) {
                        sails.log.info(location);
                      }
                      callback(null, location);
                    });
                  },
                  function (callback) {
                    Sport.findOne({id:locationSport.sport_id}).exec(function (err, sport) {
                      if (sport instanceof Error) {
                        sails.log.info(sport);
                      }
                      callback(null, sport);
                    });
                  }
                ],
                function (err, results) {
                  if (!(results[0].id in json.locations)) {
                    json.locations[results[0].id] = results[0];
                  }
                  if (!(results[1].id in json.sports)) {
                    json.sports[results[1].id] = results[1];
                  }
                }
              ); // async.parallel
            }); // locationSport
            callback();
          }, function (err) {
            sails.log.info('each');
            sails.log.info(json);
          }); // async.each
        }); // UserSport
      }
    }); // User
  }
}

您的代码结构如下:

      async.each(userSports, function (userSport, callback) {
        // Whatever happen here, it runs asyncly
        callback();
      }, function (err) {
        sails.log.info('each');
        sails.log.info(json);
      }); // async.each

您正在调用回调方法,但对数据的处理尚未完成(它正在异步运行(。因此,sails.log.info被立即调用。

您应该修改代码,以便在进程完成后调用回调。即在异步的结果中。并行:

      async.each(userSports, function (userSport, outer_callback) {
        LocationSport.findOne({id:userSport.locationsport_id}).exec(function (err, locationSport) {
          //...
          async.parallel(
            [
              function (callback) {
                // ...
              },
              function (callback) {
                // ...
              }
            ],
            function (err, results) {
              // ...
              outer_callback();
            }
          ); // async.parallel
        }); // locationSport
      }, function (err) {
        sails.log.info('each');
        sails.log.info(json);
      }); // async.each

相关内容

  • 没有找到相关文章

最新更新