🦊 for문으로 await문 돌리기(순서 보장)
const fs = require('fs');
const fs_promises = require('fs').promises;
let fileList = ['test.txt', 'test1.txt', 'test2.txt', 'test3.txt'];
const asyncFunction = async () => {
try{
for (const [i, r] of fileList.entries()){
const data = await fs_promises.readFile(r, 'utf8');
console.log(data);
}
} catch(err1) {
console.error(err1);
}
};
asyncFunction();
🦊 Promise.all로 await문 돌리기(순서를 보장하지 않지만 한번에 여러개를 돌리므로 속도가 빠름)
const fs = require('fs');
const fs_promises = require('fs').promises;
let fileList = ['test.txt', 'test1.txt', 'test2.txt', 'test3.txt'];
const asyncFunction = async () => {
try{
await Promise.all(fileList.map(async (r) => {
const data = await fs_promises.readFile(r, 'utf8');
console.log(data);
}));
} catch(err1) {
console.error(err1);
}
};
asyncFunction();
와우!!!!