創建的文件名big.file的和存儲的值:Node.js的promisifaction錯誤
const fs = require('fs');
const file = fs.createWriteStream('./big.file');
for(let i=0; i<= 2; i++) {
file.write('1\n2\n3');
}
file.end()
試過promisifaction,但我不知道是這樣還是不行。
function readFile('./big.file','utf8'){
return new Promise(function(resolve,reject){
fs.readFile('./big.file', 'utf8',function(err,data){
if(err) reject (err);
else{
console.log(data);
const lines =data.split('\n');
console.log(lines);
const numbers = lines.map(Number);
const oddNumbers = numbers.filter(n => n%2 === 1);
console.log('Odd numbers count:', oddNumbers.length);
}
});
});
};
你也應該'resolve'用'data'立即,並在'then'回調做任何進一步的處理 – Bergi