如何正确地做出一个数组中的每一项异步任务的同步列表
我需要以同步的方式执行多个任务。提取一个文件夹的一些文件和子文件夹到项目的数组,然后让所有的人的临时副本。这两个任务,我设法做他们同步。
然后,我需要在每一个阵列的项目执行7任务,我需要等待传递给下一个之前完成了一个7任务。
但每次试图通过顺序执行任务的结束:3页要处理的文件示例:
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 6 7 6 7 6 7。而我想它是:1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7持续两个任务堆叠起来,只有当5个首创任务为每个文件都NE做出触发。
在这里,我如何我启动脚本:
start();
async function start() {
await listDirToProcess(); // This works
await makeSafeCopy(); // This works as well
for (var currentDCF of dirToProcess) {
Perform the 7 tasks...
}
}
我试着用我尝试没有我或多或少异步试过的start()函数回调地狱(在以前的任务的回调每一件事)/等待键的话我也试图与一个asyncForEach功能我发现的SO代替当前换的循环:
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
但他们没有做的伎俩。
我目前使用的(XX YY的)循环来遍历数组。
下面是对这些任务的摘要: 第1步:文件复制到另一个文件夹,我这样做是这样的:
// Usage of the function
await copyToSomeDir(stuff, (err) => {
err == 0 ?
log.info('\t\tCopy to xxx went well') :
log.info('\t\tCopy to xxx ended with ' + err + " error ");
});
// Definition of the function
function copyToSomeDir(item, cb) { // Notice i didn't put the async key word
...
cb(i);
}
步骤2:等待一个外部应用程序执行第1步的输出文件夹中的作业
// Usage of the function
await execExternJob();
// Definition of the function
function execExternJob() {
log.info("Executing Talend job")
child_process.execSync(cmd_to_exe, (error, stdout, stderr) => {
log.info("job completed for the " + item.entity);
})
}
第3步:检查以前的extern工作的结果:
// Usage of the function
await checkSuccessfulJob(xxx, (list) => {
...
});
// Definition of the function
function checkSuccessfulJob(xxx, cb) {
...
cb(list);
}
第4步:更换被别人一些文件
// Usage of the function
await OverwriteSucessFile(currentDCF.talendResult.success);
// Definition of the function
function OverwriteSucessFile(list) {
...
};
!这里就是打破它所有的步骤。 !步骤5 - 6 - 7:如果不是万能的外部作业过程中很顺利,做的东西否则拉链的文件夹,并把它上传到Web服务(CMS的实际)。
if (fault) {
fault.forEach((faultyFile) => {
... // Not important
})
} else {
//Usage of the function
await prepareZipFolder(xxx, yyy, (path) => { // Step 5
getUrlFromFile(AlfrescoUrl, (url) => { // Step 6
uploadToCMS(url, path (ok) => { // Step 7
log...
})
});
});
}
这里是什么,我认为是有问题的函数的定义:
async function prepareZipFolder(zipFileName, rootZipFolder, callback) {
var zipFile = fs.createWriteStream(path.join(rootZipFolder, zipFileName) + ".zip");
// >>>>>>>>>>> Here is where it pass to the next itteration instead of waiting the end of the archiving. I think ! <<<<<<<<<<<
var archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
zipFile.on('close', () => {
callback(path.join(rootZipFolder, zipFileName) + ".zip")
})
zipFile.on('open', (fd) => {
archive.on('error', function (err) { log.info(err) });
archive.pipe(zipFile);
archive.glob("**", { cwd: path.join(rootZipFolder, zipFileName) }, { prefix: zipFileName });
archive.finalize();
})
}
问题是,在创建的.zip FIL时,循环传递到下一个项目,而不是等待归档到结束。
很抱歉的长期问题,有人可以帮忙吗?
回答如下:如果我理解正确的话,你不断地混合使用回调函数promisified功能(也有一些奇怪的事情发生像child_process.execSync(cmd_to_exe, (error, stdout, stderr) => {...
- Sync
功能不使用回调,但是这可能是一个错字)。
你需要坚持使用一种方法。如果某些API不提供返回无极功能,如果你只是他们之前添加await
这些功能不会成为无极兼容。你需要找到另一种API或自行promisify您的包装功能。
例如,您的第2步:
await execExternJob();
function execExternJob() {
log.info("Executing Talend job")
child_process.execSync(cmd_to_exe, (error, stdout, stderr) => {
log.info("job completed for the " + item.entity);
})
}
可改写为:
await execExternJob();
function execExternJob() {
log.info("Executing Talend job");
return new Promise((resolve, reject) => {
child_process.exec(cmd_to_exe, (error, stdout, stderr) => {
if (error) {
reject(error);
} else {
log.info("job completed for the " + item.entity);
resolve();
}
});
});
}
等等。
如何正确地做出一个数组中的每一项异步任务的同步列表
我需要以同步的方式执行多个任务。提取一个文件夹的一些文件和子文件夹到项目的数组,然后让所有的人的临时副本。这两个任务,我设法做他们同步。
然后,我需要在每一个阵列的项目执行7任务,我需要等待传递给下一个之前完成了一个7任务。
但每次试图通过顺序执行任务的结束:3页要处理的文件示例:
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 6 7 6 7 6 7。而我想它是:1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7持续两个任务堆叠起来,只有当5个首创任务为每个文件都NE做出触发。
在这里,我如何我启动脚本:
start();
async function start() {
await listDirToProcess(); // This works
await makeSafeCopy(); // This works as well
for (var currentDCF of dirToProcess) {
Perform the 7 tasks...
}
}
我试着用我尝试没有我或多或少异步试过的start()函数回调地狱(在以前的任务的回调每一件事)/等待键的话我也试图与一个asyncForEach功能我发现的SO代替当前换的循环:
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
但他们没有做的伎俩。
我目前使用的(XX YY的)循环来遍历数组。
下面是对这些任务的摘要: 第1步:文件复制到另一个文件夹,我这样做是这样的:
// Usage of the function
await copyToSomeDir(stuff, (err) => {
err == 0 ?
log.info('\t\tCopy to xxx went well') :
log.info('\t\tCopy to xxx ended with ' + err + " error ");
});
// Definition of the function
function copyToSomeDir(item, cb) { // Notice i didn't put the async key word
...
cb(i);
}
步骤2:等待一个外部应用程序执行第1步的输出文件夹中的作业
// Usage of the function
await execExternJob();
// Definition of the function
function execExternJob() {
log.info("Executing Talend job")
child_process.execSync(cmd_to_exe, (error, stdout, stderr) => {
log.info("job completed for the " + item.entity);
})
}
第3步:检查以前的extern工作的结果:
// Usage of the function
await checkSuccessfulJob(xxx, (list) => {
...
});
// Definition of the function
function checkSuccessfulJob(xxx, cb) {
...
cb(list);
}
第4步:更换被别人一些文件
// Usage of the function
await OverwriteSucessFile(currentDCF.talendResult.success);
// Definition of the function
function OverwriteSucessFile(list) {
...
};
!这里就是打破它所有的步骤。 !步骤5 - 6 - 7:如果不是万能的外部作业过程中很顺利,做的东西否则拉链的文件夹,并把它上传到Web服务(CMS的实际)。
if (fault) {
fault.forEach((faultyFile) => {
... // Not important
})
} else {
//Usage of the function
await prepareZipFolder(xxx, yyy, (path) => { // Step 5
getUrlFromFile(AlfrescoUrl, (url) => { // Step 6
uploadToCMS(url, path (ok) => { // Step 7
log...
})
});
});
}
这里是什么,我认为是有问题的函数的定义:
async function prepareZipFolder(zipFileName, rootZipFolder, callback) {
var zipFile = fs.createWriteStream(path.join(rootZipFolder, zipFileName) + ".zip");
// >>>>>>>>>>> Here is where it pass to the next itteration instead of waiting the end of the archiving. I think ! <<<<<<<<<<<
var archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
zipFile.on('close', () => {
callback(path.join(rootZipFolder, zipFileName) + ".zip")
})
zipFile.on('open', (fd) => {
archive.on('error', function (err) { log.info(err) });
archive.pipe(zipFile);
archive.glob("**", { cwd: path.join(rootZipFolder, zipFileName) }, { prefix: zipFileName });
archive.finalize();
})
}
问题是,在创建的.zip FIL时,循环传递到下一个项目,而不是等待归档到结束。
很抱歉的长期问题,有人可以帮忙吗?
回答如下:如果我理解正确的话,你不断地混合使用回调函数promisified功能(也有一些奇怪的事情发生像child_process.execSync(cmd_to_exe, (error, stdout, stderr) => {...
- Sync
功能不使用回调,但是这可能是一个错字)。
你需要坚持使用一种方法。如果某些API不提供返回无极功能,如果你只是他们之前添加await
这些功能不会成为无极兼容。你需要找到另一种API或自行promisify您的包装功能。
例如,您的第2步:
await execExternJob();
function execExternJob() {
log.info("Executing Talend job")
child_process.execSync(cmd_to_exe, (error, stdout, stderr) => {
log.info("job completed for the " + item.entity);
})
}
可改写为:
await execExternJob();
function execExternJob() {
log.info("Executing Talend job");
return new Promise((resolve, reject) => {
child_process.exec(cmd_to_exe, (error, stdout, stderr) => {
if (error) {
reject(error);
} else {
log.info("job completed for the " + item.entity);
resolve();
}
});
});
}
等等。