Your return new Promise()
is not returning from readFile()
. Because asynchronous operations in node.js are non-blocking, the readfile()
function returns immediately (with no return value) and then the file event occurs sometime later (after the function has already returned). So your readFile()
function returns nothing, therefore await has no promise to actually wait on.
This shows that you need some basic education/reading on how asynchronous events and operations work in node.js.
The return new Promise()
you do have just goes back into the readLine library file system event handling code and is ignored so it doesn't do anything useful. It's not actually returned to anyone who's looking for a promise.
So, since you aren't actually returning anything and you aren't actually await
ing anything, your two async operations proceed in parallel and it is indeterminate which one completes first. It will likely depend upon the details of how long each readFile()
takes and it's just a race to see which one finishes first. You have not controlled their order of execution.
The order of events is this sequence:
- Start first
readFile()
operation. It returns no return value and the file operations in it proceed as asynchronous code/events.
- You
await undefined
which doesn't wait for anything.
- Start second
readFile()
operation. It returns no return value and the file operations in it proceed as asynchronous code/events.
- You
await undefined
which doesn't wait for anything.
- One of the two
readFile()
operations generates its close event (whichever one happens to finish first).
- The other
readFile()
operations generates its close event.
You can get readFile()
to actually return a promise that is connected to when it completes which you can then await
like this:
const fs = require('fs');
const readline = require('readline');
function readfile(file) {
return new Promise(function(resolve, reject) {
var fReadAD = fs.createReadStream(file,{ encoding: 'utf8'});
var objReadlineAD = readline.createInterface({
input: fReadAD
});
objReadlineAD.on('close', () => {
console.log('file:'+file)
resolve();
}).on('error', reject);
});
}
(async () => {
await readfile('./import/1.xls')
await readfile('./import/2.csv')
})();