我需要打开一个文件,解析它,发出http请求并将数据保存到另一个文件中。如果不将所有代码嵌套在一个地方,我可以这样做吗? (它使代码的可读性降低。)
我想最终有类似的东西。写入全局范围是否可以?
on_write_file = function(err, write_file) {
// parse the data from the opening file
};
on_read_file = function(err, read_file) {
fs.open('end_file', 'w', on_write_file);
};
fs.open('start_file', 'r', on_read_file);
在Web服务器中,使用全局变量是危险的,那我该怎么办?
答案 0 :(得分:0)
是的,您可以使用.bind
将数据加入到函数中。
on_write_file = function(read_file, err, write_file) {
// parse the data from the opening file
};
on_read_file = function(err, read_file) {
fs.open('end_file', 'w', on_write_file.bind(null, read_file));
};
fs.open('start_file', 'r', on_read_file);