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();
}