Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Node.js的readFile方法中处理超时回调事件_Node.js_Asynchronous - Fatal编程技术网

在Node.js的readFile方法中处理超时回调事件

在Node.js的readFile方法中处理超时回调事件,node.js,asynchronous,Node.js,Asynchronous,我的要求如下: 我有3个大文件大文件1,大文件2和大文件3。我希望异步读取这3个大文件,并仅在读取完所有文件后处理其余代码 fs.readFile('big_file_1', function (err, data) { if (err) throw err; content_1 = data; }); fs.readFile('big_file_2', function (err, data) { if (err) throw err; content_2 = data; })

我的要求如下:

我有3个大文件大文件1,大文件2和大文件3。我希望异步读取这3个大文件,并仅在读取完所有文件后处理其余代码

fs.readFile('big_file_1', function (err, data) {
  if (err) throw err;
  content_1 = data;
});
fs.readFile('big_file_2', function (err, data) {
  if (err) throw err;
  content_2 = data;
});
fs.readFile('big_file_3', function (err, data) {
  if (err) throw err;
  content_3 = data;
});
// Do something with content_1, content_2 and content_3

如何在Node.JS中实现这一点?

您可以使用以下的
并行功能来实现:

或者,您可以手动执行此操作:

int steps_done = 0
fs.readFile('big_file_1', function(err, data) {
  if (err) throw err
  content_1 = data
  if (++steps_done == 3) do_next_step()
})
fs.readFile('big_file_2', function(err, data) {
  if (err) throw err
  content_2 = data
  if (++steps_done == 3) do_next_step()
})
fs.readFile('big_file_3', function(err, data) {
  if (err) throw err
  content_3 = data
  if (++steps_done == 3) do_next_step()
})

听起来像是一份工作。这就是我需要的。谢谢@thejh
int steps_done = 0
fs.readFile('big_file_1', function(err, data) {
  if (err) throw err
  content_1 = data
  if (++steps_done == 3) do_next_step()
})
fs.readFile('big_file_2', function(err, data) {
  if (err) throw err
  content_2 = data
  if (++steps_done == 3) do_next_step()
})
fs.readFile('big_file_3', function(err, data) {
  if (err) throw err
  content_3 = data
  if (++steps_done == 3) do_next_step()
})