gpt4 book ai didi

javascript - node.js 等待任务完成

转载 作者:行者123 更新时间:2023-12-03 11:45:09 25 4
gpt4 key购买 nike

所以我正在编写这个node.js 程序来将XML 文件导入到JSON 对象数组中。我有 2 个文件要导入,teachers.xml 和 Students.xml。

教师和学生包含数千条有关教师和学生的信息。我的代码很好地涵盖了该部分。

这是我用于解析文件的 javascript 文件:

var fs = require('fs');
var xmldom = require('xmldom');
var async = require('async');

// Parse `catalog` xml file and call `callback(catalog domxml root)`
function parse_catalog(catalog, callback) {
// Read xml file content
fs.readFile(catalog, function (err, data) {
if (err) {
throw 'Error reading XML catalog';
} else {
// Parse xml content and return dom root
var domRoot = new xmldom.DOMParser().parseFromString(data.toString());
// Call callback passing dom root
callback(domRoot)
}
});
}

我有两种这样的方法将 xml 转换为 json 并且它工作得很好(一种用于教师,一种用于学生)

// Convert teacher XML into json format in a array
function convert_teachers(domCatalog) {
var teachers = domCatalog.getElementsByTagName('teacher');
var teachers_arr= [];
for (var i = 0; i < teachers .length; i++) {
var teacher= teachers[i];
...
//Reading the xml

teachers_arr.push({
...
//Create the json

});
}
console.log("Teachers are now in JSON format ");
}

所以最后我要做的就是:

parse_catalog('teachers.xml', convert_teachers);

当我这样做时:

parse_catalog('teachers.xml', convert_teachers);
parse_catalog('students.xml', convert_students);

其中一个将首先完成,具体取决于要导入的元素数量,我认为这很正常。

我想要的是等待两者都被导入,然后进行一些 JavaScript 操作,这就是我陷入困境的地方。

我尝试使用异步执行此操作,但它不会等到两个文件完成导入。

async.parallel([
function(callback) {
parse_catalog('teachers.xml', convert_teachers);
callback();
},

function(callback) {
parse_catalog('students.xml', convert_students);
callback();
}
], function(err) {
if (err) return next(err);

console.log("Finished");
//Enventually some Javascript manipulations on the two arrays

});

实际上它输出:

Finished
Teachers are now in JSON format
Students are now in JSON format

或取决于文件大小

Finished
Students are now in JSON format
Teachers are now in JSON format

我想要的更像是:

Teachers are now in JSON format (or students)
Students are now in JSON format (or teachers)
Finished

我计划再加载 2 个文件,它们的加载顺序对我来说并不重要。

有任何线索吗?谢谢!

最佳答案

您在 async.parallel() 函数中执行 callback() 的速度太快了,因为到那时 fs.readFile()还没开始呢。尝试这样的事情:

function parse_catalog(catalog, callback) {
// Read xml file content
fs.readFile(catalog, function(err, data) {
if (err)
return callback(err);

// Parse xml content and return dom root
var domRoot = new xmldom.DOMParser().parseFromString(data.toString());

// Call callback passing dom root
callback(null, domRoot);
});
}

// Convert teacher XML into json format in a array
function convert_teachers(domCatalog) {
var teachers = domCatalog.getElementsByTagName('teacher');
var teachers_arr = [];
for (var i = 0; i < teachers .length; i++) {
var teacher = teachers[i];
...
//Reading the xml

teachers_arr.push({
...
//Create the json

});
}
console.log('Teachers are now in JSON format');
return teachers_arr;
}
// and similarly for `convert_students`

async.parallel({
teachers: function(callback) {
parse_catalog('teachers.xml', function(err, domCatalog) {
if (err)
return callback(err);
var teachers = convert_teachers(domCatalog);
callback(null, teachers);
});
},
students: function(callback) {
parse_catalog('students.xml', function(err, domCatalog) {
if (err)
return callback(err);
var students = convert_students(domCatalog);
callback(null, students);
});
}
}, function(err, results) {
if (err) return next(err);

console.log('Finished');

// here you have `results.teachers` and `results.students`
console.dir(results);
});

关于javascript - node.js 等待任务完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26088432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com