0
for (var i = 0; i < users.length; i++) {
  if (users[i].access_token !== undefined) {
    var syncer = require('dropsite_server/dbox_sync');
    var client = dboxApp.client(users[i].access_token);
    var websites_to_sync = [];
    syncer.doSync(client, users[i].website, users[i].access_token.uid);
  }
}

在 syner.doSync 我有一些异步函数调用。是的,出于性能原因,我想让它们保持异步。问题是,同步器对象只是一个引用,所以当 doSync 工作时,for 循环继续运行,然后更改变量。

现在周围的 for 循环只有两个元素,结果是,只有最后一个元素被处理,但不是一次,而是两次。

解决方案是使同步器成为一个合适的对象,但不知何故我失败了。为了更好地理解同步器中的代码是什么样的,这里开始:

/**
* This here is the sync module. User needs to provide. All this module does,
* is sync from a users dropbox to a local path
*/
var dbox = require("dbox");
var fs = require("fs");
var async = require("async");

var allow_downloads = true;
var allow_local_deletes = true;

var client = null;
var saved_sync_data = null;
var sync_data_path = global.config.sync_data_file;
var uid = null;
var remote_sync_dirs = null;

//var sync_data_file = ".dropbox_sync_data";

var errors = [];

var queue = async.queue(doTask, 1);

exports.doSync = function (clientIn, website_domain, _uid) {
    client = clientIn;
    uid = _uid; 
    sync_data_path = sync_data_path + "_" + uid;
    remote_sync_dirs = website_domain;
    async.series(
      [
        readSyncDataFile,
        startSync
      ]);
}
/**
 * Start the Sync
 * @param  dbox client This is a dbox client
*/

function startSync() {
  console.log("get remote delta for website: " + remote_sync_dirs)
  getRemoteDelta();
}
4

1 回答 1

0

var 声明不限于循环,应在文件/函数的顶部进行

var syncer = require('dropsite_server/dbox_sync')
  , client
  , websites_to_sync = [] //what is this used for?

for (var i = 0; i < users.length; ++i) {
  if (users[i].access_token !== undefined) {
    client = dboxApp.client(users[i].access_token)
    syncer.doSync(client, users[i].website, users[i].access_token.uid);
  }
}

最后一项被处理两次的原因是 doSync 函数设置了每次调用它时都会覆盖的模块级变量。

解决此问题的方法是将变量传递给函数

exports.doSync = function (client, website_domain, uid) {
  sync_data_path = sync_data_path + "_" + uid
  remote_sync_dirs = website_domain

  async.series([
      readSyncDataFile.bind(null, client, website_domain, uid) 
    , startSync.bind(null, client, website_domain, uid) 
  ])
}

function startSync(client, website_domain, uid) {
  ...
}
function readSyncDataFile(client, website_domain, uid) {
  ...
}
于 2013-07-29T03:18:14.493 回答