Using filesystem in node.js with async / await

Native support for async/await fs functions since Node 11

Since Node.JS 11.0.0 (stable), and version 10.0.0 (experimental), you have access to file system methods that are already promisify’d and you can use them with try catch exception handling rather than checking if the callback’s returned value contains an error.

The API is very clean and elegant! Simply use the .promises member of fs object:

import fs from 'fs';

async function listDir() {
  try {
    return await fs.promises.readdir('path/to/dir');
  } catch (err) {
    console.error('Error occurred while reading directory!', err);
  }
}

listDir();

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)