因为
for
循环不是异步的,但
copy
是,所以您不能使用
对于
以遍历异步函数。解决方案正在使用
async
图书馆
https://github.com/caolan/async
异步。map(arr,iterate,[callback])
例如,您可以尝试:
async.map(photoOrder, function (photo, callback) {
console.log('Moving Photo');
fs.copy(cfg.watchedFolder + photo + '.jpg', cfg.outputFolder + sessionNumber + "/" + i + '.jpg', function (err) {
if (err) {
// i.e. file already exists or can't write to directory
callback(err);
}
});
}, function (err, results) {
nextCommand();
});