I'm new to node.js and stackoverflow and I'm having a little trouble : I want to read two files and do something with them in a specific order. Problem being that i don't know wich one will finish being read first so I don't know how to make sure they will trigger in the right order.
To give an example let's say that I want to read two files and write them in the response :
fs.readFile('./file1',(err,data) => {
res.write(data);
})
fs.readFile('./file2',(err,data) => {
res.write(data);
})
How could I make sure the first file will be writen before the second even if the second file is smaller than the first one ?
I could do that :
fs.readFile('./file1',(err,data) => {
res.write(data);
fs.readFile('./file2',(err,data) => {
res.write(data);
})
})
But it would act like a blocking structure : the second one couldn't start being read before the end of the first one and that's not the point of Node.js... Am I right ?
PS : Sorry if the question is dumb or for my poor english
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…