async和await改寫node.js的讀取檔案函式
阿新 • • 發佈:2019-02-05
const fs = require('fs');
const path = require('path');
function getFile() {
return new Promise((resolve, reject) => {
const filePath = path.resolve(path.join(__dirname, '../public/images'));
fs.readdir(filePath, (err, files) => {
var fileArr = [];
files. forEach(filename => {
fileArr.push('/static/images/' + filename);
resolve(fileArr)
});
})
});
}
async function getImagesPath() {
let result = await getFile();
return result;
}
function insertDbData(){
getImagesPath().then(res => {
console.log(res)
})
}
insertDbData()