1. 程式人生 > >async和await改寫node.js的讀取檔案函式

async和await改寫node.js的讀取檔案函式

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()